示例
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdbool.h> #define LEN 20 struct names { char first[LEN]; char last[LEN]; }; struct guy { struct names handle; //嵌套结构 char favfood[LEN]; char job[LEN]; float income; }; int main(int argc, char* argv[]) { // 初始化一个结构变量 struct guy fellow = { { "Ewen", "Villard" }, "grilled salmon", "personality coach", 68112.00 }; //结构体直接赋值 struct guy fellow1 = fellow;//内存复制(按值传递) fellow.job[0] = 'X'; printf("first=%s, last=%s\n", fellow1.handle.first, fellow1.handle.last); printf("favfood=%s, job=%s\nincome=%.2f\n", fellow1.favfood, fellow1.job, fellow1.income); system("pause"); return 0; }
运行测试