1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include<stdio.h> int main() { // 1、定义结构体类型Person struct Person { int age; float height; char* name; }; // 2、定义结构体类型Person的变量 p1,p2,p3 // p1 按定义顺序,设置默认值 struct Person p1 = {18,17.5,"xiaohuo"}; // p2 按结构体类型的基本变量,设置值 struct Person p2 = {.name="xiaohong",.age=19,.height=16}; // p3 先定义,后初始化 struct Person p3; p3.name = "xiaobei"; p3.age = 20; p3.height = 170;
// 3、输出结构体类型变量数据,这里以p1为例 printf("==============结构体变量p1的内存字节数=============\n"); printf("p1的内存字节数:%d\n",sizeof(p1)); printf("==============结构体变量p1的内存地址===============\n"); printf("p1=%p\n",&p1); printf("p1.age=%p\n",&p1.age); printf("p1.height=%p\n",&p1.height); printf("p1.name=%p\n",&p1.name); printf("==============结构体变量p1的数据内容===============\n"); printf("p1.age=%d\n",p1.age); printf("p1.height=%f\n",p1.height); printf("p1.name=%s\n",p1.name); return 0; }
|