- #include <stdio.h>
- int add();
- int main()
- {
- //第一种定义及初始化赋值
- struct lv1
- {
- char *name;
- int age;
- };
- struct lv1 l1={"吕1",3};
- printf("l1.name=%s\tl1.age=%d\n",l1.name,l1.age);
- l1.name="国6"; //改变l1的name的值
- l1.age=8; //改变l1的age的值
- printf("l1.name=%s\tl1.age=%d\n",l1.name,l1.age);
- //第二种定义及初始化赋值
- struct lv2
- {
- char *name;
- int age;
- } l2={"国2",4},l3={"栋3",5};
- printf("l2.name=%s\tl2.age=%d\nl3.name=%s\tl3.age=%d\n",l2.name,l2.age,l3.name,l3.age);
- //第三种定义及初始化赋值
- struct lv3
- {
- char *name;
- int age;
- } l4,l5;
- l4.name="国4";
- l4.age=6;
- l5.name="吕5";
- l5.age=7;
- printf("l4.name=%s\tl4.age=%d\nl5.name=%s\tl5.age=%d\n",l4.name,l4.age,l5.name,l5.age);
- //匿名结构体,会出现警告,但不会影响程序正常运行
- struct
- {
- char *name;
- int age;
- };
- add();
- add1();
- return 0;
- }
- //定义全局结构体
- struct lv4
- {
- char *name;
- int age;
- };
- int add()
- {
- struct lv4 l6={"吕6",9};
- printf("l6.name=%s\tl6.age=%d\n",l6.name,l6.age);
- return 0;
-
- }
- int add1()
- {
- //定义局部结构体
- struct lv5
- {
- char *name;
- int age;
- };
- struct lv5 l7={"国7",8};
- printf("l7.name=%s\tl7.age=%d\n",l7.name,l7.age);
- return 0;
- }
复制代码
|
|