struct tag{ member_list; }; //使用 struct tag tt; //定义即声明变量 struct tag{ member_list; }val_list;
struct { member_list; }val_list;
typedef struct { member_list; }STR; //自引用定义:因为有指针指向自己,不能采用不具名定义 typedef struct str { member_list; struct str *other; } STR;
#include <stdio.h> #include <string.h> typedef struct { int year; int mon; int day; } Birth; typedef struct { int id; char name[6]; Birth birth; } Stu; int main(void) { Stu stu = {1, "guilin", {2022, 12, 20}}; printf("%d\t%s\t%d-%d-%d\n", stu.id, stu.name, stu.birth.year, stu.birth.mon, stu.birth.day); Stu *p = &stu; printf("%d\t%s\t%d-%d-%d\n", p->id, p->name, (p->birth).year, (p->birth).mon, (p->birth).day); //部分赋值 stu.id = 101; strcpy(stu.name, "pla"); printf("%d\t%s\t%d-%d-%d\n", stu.id, stu.name, stu.birth.year, stu.birth.mon, stu.birth.day); //使用IO修改 scanf("%d%s", &stu.id, stu.name); }
数据类型 | 地址倍数 |
char | 1 |
int | 4 |
float | 4 |
double | 8 |
typedef struct { char a; short b; int c; } A;
A | Byte | Byte | Byte | Byte |
char a | short b | |||
int c |
typedef struct { int c; short b; char a; } B;
B | Byte | Byte | Byte | Byte |
int c | ||||
short b | char a |
typedef struct { char a; int c; short b; } C;
C | Byte | Byte | Byte | Byte |
char a | ||||
int c | ||||
short b |
typedef struct { char a; char d; short b; int c; } D;
D | Byte | Byte | Byte | Byte |
char a | char d | short b | ||
int c |
typedef struct { char a; short b; char d; int c; } E;
E | Byte | Byte | Byte | Byte |
char a | short b | |||
char d | ||||
int c |
typedef struct { char a; short b; int c; char d; short f; char e; } F;
F | Byte | Byte | Byte | Byte |
char a | short b | |||
int c | ||||
char d | short f | |||
char e |