@fiy-fish
2015-05-17T13:37:40.000000Z
字数 807
阅读 1760
c
先上代码!
#include<stdio.h>#include<stdlib.h>//包含了system("pause")#define LEN 20struct names{char first[LEN];char last[LEN];};struct guy{struct names handle;//结构体嵌套char favfood[LEN];char job[LEN];float income;};int main(void){struct guy fellow[2]={ //结构数组的初始化{{"Ewen","Villard"},"grilled salmon","personality coach",58112.00},{{"Rodney","Swillbelly"},"tripe","tabloid editor",232400.00}};struct guy * him;/*这是一个指向结构的指针 */printf("address #1: %p #2: %p\n", &fellow[0],&fellow[1]);him = &fellow[0]; /*告诉该指针它要指向的地址*/printf("pointer #1: %p #2: %p\n", him,him + 1);printf("him->income is $%.2f: (*him).income is $%.2f\n",him->income,(*him).income);him++;//指向下一个结构printf("him->favfood is %s: him->handle.last is %s\n",him->favfood,him->handle.last);system("pause");return 0;}
用指针访问结构体成员
him->income
(*him).income
上面这两句话是一个意思,这里出现一个新的运算符->,意思是通过指针him指向结构体的incume成员
