ios-c_day13___ Structural Body

Source: Internet
Author: User

2015.2.4

#include <stdio.h>

#include <string.h>

struct

struct Goods

//{

Char name[20];

float price;

Long amount;

//};

//

int main (int argc, const char * argv[]) {

struct Goods book;

strcpy (Book.name, "kingdoms");

printf ("Len =%ld\n", strlen (Book.name));

Book.price = 12.8;

Book.amount = 1000;

//

printf ("Name:%s Price:%.2f Amount:%ld\n", Book.name, Book.price,book.amount);

//

return 0;

//}

Declaring a data type

typedef struct STUDENT

//{

Char name[20];

int age;

int num;

}stu;

//

int main (int argc,const char *argv[])

//{

STU STU;

strcpy (Stu.name, "Xiao Hong");

Stu.age = 12;

Stu.num = 50;

printf ("Name:%s\nage:%d\nnum:%d\n", stu.name,stu.age,stu.num);

//

return 0;

//}

1. Use a

typedef unsigned char uint_8;

typedef unsigned short uint_16;

typedef unsigned int uint_32;

typedef unsigned long uint_64;

//

typedef int INT_32;

//

int main (int argc,const char *argv[])

//{

Uint_32 a=100;

printf ("A =%d\n", a);

//

return 0;

//}

2. Alias a struct

3. Declaring Complex types

Int (*p) (int,int);

typedef int (*PFUNC) (int,int);//int (*) (Int,int)

//

int add (int a, int b)

//{

return a+b;

//}

//

int main (int argc,const char *argv[])

//{

Pfunc P=add;

printf ("%d\n", p (3,5));

//

return 0;

//}

Name of the province about structure

Common Methods 1

Struct names can be the same as data type names

typedef struct student{

Char name[20];

int age;

}student;

Common Method Two

typedef struct {

Char name[20];

int age;

}student;

int main (int argc, const char *argv[])

//{

struct Student stu={"Little Red", 12};

//

printf ("name =%s, age =%d\n", stu.name,stu.age);

//

return 0;

//}

struct-Body pointer

typedef struct {

Char name[20];

int age;

}student,*pstudent;

To access member variables in a struct by using the-I operator

int main (int argc, const char *argv[])

//{

Pstudent pstu = NULL;

Student stu={"Xiao Hua", 22};

Pstu = &stu;

//

printf ("name =%s age =%d\n", pstu->name,pstu->age);

return 0;

//}

struct array

int main (int argc,const char *argv[])

//{

Student *pstu;

Student Stu;

Pstu = &stu;

//

scanf ("%s%d", stu.name,&stu.age);

scanf ("%s%d", pstu->name,&pstu->age);

//

printf ("name =%s, age =%d\n", stu.name,stu.age);

return 0;

//}

int main (int argc,const char *argv[])

//{

Student stu[5]={{"Xiao Hua", 12},{"small Music", 8},{"Floret", 10},{"small Fly", 14},{"small Yellow", 7};

//

for (int i=0; i<5-1; i++) {

for (int j=0; j<5-i-1; J + +) {

if (Stu[j].age > Stu[j+1].age) {

Student Temp=stu[j];

STU[J]=STU[J+1];

Stu[j+1]=temp;

//            }

//        }

//    }

for (int i=0; i<5; i++) {

printf ("%s-----%d\n", stu[i].name,stu[i].age);

//    }

//

return 0;

//}

int main (int argc,const char *argv[])

//{

Student stu[5]={{"Xiao Hua", 12},{"small Music", 8},{"Floret", 10},{"small Fly", 14},{"small Yellow", 7};

Pstudent pstu = stu;

//

for (int i=0; i<5-1; i++) {

for (int j=0; j<5-i-1; J + +) {

if ((pstu+j)->age > (pstu+j+1)->age) {

Student temp=* (PSTU+J);

* (PSTU+J) =* (pstu+j+1);

* (pstu+j+1) =temp;

//            }

//        }

//    }

for (int i=0; i<5; i++) {

printf ("%s-----%d\n", (Pstu+i)->name, (pstu+i)->age);

//    }

//

return 0;

//}

Structure nesting

The wording of a

typedef struct {

Char name[20];

int age;

struct Date

//    {

int year;

int month;

int day;

}date;//declaring a variable directly when declaring a data type

char sex;

}employer;

Two

typedef struct DATE

//{

int year;

int month;

int day;

}date;

//

typedef struct {

Char name[20];

int age;

Date date;

char sex;

}employer;

//

//

int main (int argc,const char *argv[])

//{

Employer xiaohua={"Xiao Hua", 12,{2014,12,4},1};

printf ("name:%s age:%d%d months%d days, gender:%d\n", Xiaohua.name,xiaohua.age, Xiaohua.date.year,xiaohua.date.month, Xiaohua.date.day, Xiaohua.sex);

//

//

return 0;

//}

Declaring struct-body types

struct student{

Char name[20];

int age;

//};

Declaring a struct type defines a variable at the same time

struct Student

//{

Char name[20];

int age;

}xiaohong={"Xiao Hong", 15},xiaohua;

struct

//{

Char name[20];

int age;

}xiaohong={"Xiao Hong", 15},xiaohua;

3. Name the structure

typedef struct {

Char name[20];

int age;

}student;

//

int main (int argc,const char *argv[])

//{

struct Student stu;

Student Xiaohong;

//

printf ("%s%d\n", xiaohong.name,xiaohong.age);

//

//

return 0;

//}

Structure size

is not the sum of member variables, which is the size of the memory after the member variable is aligned

typedef struct {

Double F2;

Char name[15];

int A;

Long B;

Long double L;

float F;

}structsize;

int main (int argc,const char *argv[])

{

printf ("%ld\n", sizeof (StructSize));

return 0;

}

ios-c_day13___ Structural Body

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.