[关闭]
@fiy-fish 2015-05-17T13:37:40.000000Z 字数 807 阅读 1463

结构体的嵌套使用

c


先上代码!

  1. #include<stdio.h>
  2. #include<stdlib.h>//包含了system("pause")
  3. #define LEN 20
  4. struct names{
  5. char first[LEN];
  6. char last[LEN];
  7. };
  8. struct guy{
  9. struct names handle;//结构体嵌套
  10. char favfood[LEN];
  11. char job[LEN];
  12. float income;
  13. };
  14. int main(void)
  15. {
  16. struct guy fellow[2]={ //结构数组的初始化
  17. {{"Ewen","Villard"},
  18. "grilled salmon",
  19. "personality coach",
  20. 58112.00
  21. },
  22. {{"Rodney","Swillbelly"},
  23. "tripe",
  24. "tabloid editor",
  25. 232400.00
  26. }
  27. };
  28. struct guy * him;/*这是一个指向结构的指针 */
  29. printf("address #1: %p #2: %p\n", &fellow[0],&fellow[1]);
  30. him = &fellow[0]; /*告诉该指针它要指向的地址*/
  31. printf("pointer #1: %p #2: %p\n", him,him + 1);
  32. printf("him->income is $%.2f: (*him).income is $%.2f\n",him->income,(*him).income);
  33. him++;//指向下一个结构
  34. printf("him->favfood is %s: him->handle.last is %s\n",him->favfood,him->handle.last);
  35. system("pause");
  36. return 0;
  37. }

用指针访问结构体成员
him->income
(*him).income
上面这两句话是一个意思,这里出现一个新的运算符->,意思是通过指针him指向结构体的incume成员

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注