1.结构体
1 #include <stdio.h> 2 3 //创建结构体File 4 struct File { 5 char *name; 6 int size; 7 }; 8 9 //创建结构体People 用typedef自定义 10 struct _People { 11 char *name; 12 int age; 13 }; 14 15 typedef struct _People People; 16 17 //创建结构体Person 用typedef自定义 写在一起 18 typedef struct _P { 19 char *name; 20 int age; 21 } P; 22 23 int main() { 24 25 //初始化File 26 struct File file; 27 file.name = "Clion"; 28 file.size = 100; 29 30 printf("File name is %s,File size is %d.\n", file.name, file.size); 31 32 //--------------------------------------------------------------------- 33 People people = {"Susan", 20}; 34 35 printf("Name is %s,Age is %d.\n", people.name, people.age); 36 37 //--------------------------------------------------------------------- 38 P p = {"Alex", 22}; 39 printf("Name is %s,Age is %d.\n", p.name, p.age); 40 41 42 return 0; 43 }
2.结构体指针
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 typedef struct _Student { 5 const char *name; 6 int age; 7 } Student; 8 9 void func() { 10 Student *s4 = malloc(sizeof(Student)); 11 s4->name = "Lucy"; 12 s4->age = 18; 13 Student *s5 = s4; 14 s4->age = 20; 15 printf("(%s, %d),(%s, %d)\n", s4->name, s4->age, s5->name, s5->age); 16 } 17 18 Student *createStudent(char *name, int age) { 19 Student *s = malloc(sizeof(Student)); 20 s->name = name; 21 s->age = age; 22 return s; 23 } 24 25 void deleteStudent(Student *p) { 26 free(p); 27 } 28 29 int main() { 30 31 Student s1 = {"Jim", 16}; 32 Student s2 = s1; 33 printf("(s1.name = %s,s1.age = %d),(s2.name = %s,s2.age = %d)\n", s1.name, s1.age, s2.name, s2.age); 34 s2.age = 20; 35 printf("(s1.name = %s,s1.age = %d),(s2.name = %s,s2.age = %d)\n", s1.name, s1.age, s2.name, s2.age); 36 37 printf("\n"); 38 //结构体指针 39 Student *s3 = &s1; 40 s1.age = 30; 41 printf("(s1.name = %s,s1.age = %d),(s3.name = %s,s3.age = %d)\n", s1.name, s1.age, s3->name, s3->age); 42 43 //----------------------------------------------------------------------------------------------------- 44 printf("\n"); 45 func(); 46 47 //----------------------------------------------------------------------------------------------------- 48 printf("\n"); 49 Student *s = createStudent("Daisy", 22); 50 printf("%s, %d\n", s->name, s->age); 51 deleteStudent(s); 52 53 54 return 0; 55 }
3.共同体
1 #include <stdint.h> 2 #include <stdio.h> 3 4 //共同体长度 5 typedef union _BaseData { 6 char ch; 7 uint8_t ch_num; 8 uint32_t num;//添加后共同体的长度sizeof(BaseData) 变为4 -->共同体的长度由最大成员的长度决定 9 } BaseData; 10 11 //结构体长度 12 typedef struct _Data { //内存对齐 13 uint8_t a; //1 Data长度1 14 uint8_t b; //1 Data加上后长度2 15 uint32_t c; //4 Data加上后长度8 16 uint8_t d; //1 Data加上后长度12 17 }__attribute__((__packed__)) Data; //__attribute__((__packed__))结构体压缩 18 19 //结构体和共同体结合实例 提取颜色通道 20 typedef struct _ColorARGB { 21 uint8_t blue; 22 uint8_t green; 23 uint8_t red; 24 uint8_t alpha; 25 } ColorARGB; 26 27 typedef union _Color { 28 uint32_t color; //0xFFFEED2B 存储0x2B 0xED 0xFE 0xFF 存储顺序是反的 29 ColorARGB colorArgb; 30 } Color; 31 32 int main() { 33 34 BaseData data; 35 data.ch = 'B'; 36 printf("ch_num is %d\n", data.ch_num); 37 printf("Length of BaseData is %ld\n", sizeof(BaseData)); 38 39 //--------------------------------------------- 40 printf("\n"); 41 printf("Length of Data is %ld\n", sizeof(Data)); 42 43 //----------------------------------------------- 44 printf("\n"); 45 Color c; 46 c.color = 0xFFFEED2B; 47 printf("alpha = 0x%X,red = 0x%X,green = 0x%X,blue = 0x%X.\n",c.colorArgb.alpha,c.colorArgb.red,c.colorArgb.green,c.colorArgb.blue); 48 49 return 0; 50 }