标签:
A. 基本知识
1 struct Student
2 {
3 int age;
4 char *name;
5 float height;
6 };
1 //define a struct variable
2 struct Student stu = {27, "simon", 1.65f};
1 struct Student {
2 int age;
3 char *name;
4 float height;
5 } stu = {25, "simon", 1.65f};
1 /*错误写法
2 struct Student p;
3 p = {17, "Tom"};
4 */
1 struct Student p; 2 p.age = 17; 3 p.name = "Tom”;
1 struct Student p2 = {17, "Sam”};
1 struct Student p3 = {.name="Judy", .age= 44};
1 struct
2 {
3 int age;
4 char *name;
5 } stu3;
1 void test1()
2 {
3 struct Date
4 {
5 int year;
6 int month;
7 int day;
8 };
9
10 struct Student
11 {
12 int age;
13 struct Date birthday;
14 };
15
16 struct Student stu = {25, {1989, 8, 10}};
17
18 printf("my birthday ==> %d-%d-%d\n", stu.birthday.year, stu.birthday.month, stu.birthday.day);
19 }
1 struct Student
2 {
3 int age;
4 char *name;
5 float height;
6 } stus[5];
1 struct
2 {
3 int age;
4 char *name;
5 float height;
6 } stus[5];
1 void test4()
2 {
3 struct Person p = {33, "ss"};
4 struct Person *pointer;
5
6 pointer = &p;
7
8 printf("test4: person‘s age = %d\n", p.age);//It‘s p!!!, not pointer!!
9 printf("test4: person‘s age = %d\n", (*pointer).age);
10 printf("test4: person‘s age = %d\n", pointer->age);
11 }
1 struct Student p2 = {17, "Sam"};
2 struct Student p3 = {.name="Judy", .age= 44};
3 p3 = p2;
4 printf("p3: age->%d, name-.%s\n", p3.age, p3.name);
标签:
原文地址:http://www.cnblogs.com/wvqusrtg/p/4500787.html