#include "stdio.h"
typedef struct
{
unsigned char one;
unsigned char two;
}week;
week week1;
week *pweek1;
int main()
{
week1.one = 1;
week1.two = 2;
printf("week1.one = %d\n", week1.one);
printf("week1.two = %d\n", week1.two);
pweek1 = &week1;
pweek1 -> one = 3;
pweek1 -> two = 4;
printf("week1.one = %d\n", week1.one);
printf("week1.two = %d\n", week1.two);
}
借鉴
二、以下是 结构体数组
#include "stdio.h"
typedef struct
{
unsigned short year;
unsigned char mon;
unsigned char day;
}Data;
int main()
{
unsigned char i = 0;
Data data[3] = { {2024, 2, 14}, {2024, 2, 15}, {2024, 2, 16} };
for(i = 0; i < 3; i++)
printf("%d-%d-%d\r\n",data[i].year,data[i].mon,data[i].day);
}
三、结构体赋值错误
// Student stu1;
// stu1 = { "zhangsan", 'm', 1, {1999, 10, 28} };
/* 不可以这么写,和平常定义的 char int 等类型不一样,char int 类型
等常规数据类型由编译器自动识别,而自定义的数据类型(楼 主 自定义的结构体类型),
编译器在编译阶段无法识别,故出错 */
#include "stdio.h"
typedef struct
{
unsigned short year;
unsigned char mon;
unsigned char day;
}Data;
typedef struct
{
unsigned char name [20];
unsigned char sex;
unsigned char num;
Data birthday;
}Student;
int main()
{
// Student stu1;
// stu1 = { "zhangsan", 'm', 1, {1999, 10, 28} };
/* 不可以这么写,和平常定义的 char int 等类型不一样,char int 类型
等常规数据类型由编译器自动识别,而自定义的数据类型(楼主自定义的结构体类型),
编译器在编译阶段无法识别,故出错 */
Student stu1 = { "zhangsan", 'm', 1, {1999, 10, 28} };
printf("name = %s, sex = %c, num = %d, %d-%d-%d\n", stu1.name, stu1.sex, stu1.num, stu1.birthday.year, stu1.birthday.mon, stu1.birthday.day);
}