776

1、指向结构的指针通常比结构本身更容易控制。

2、早期结构不能作为参数传递给函数,但可以传递指向结构的指针。

3、即使可以传递结构,传递指针通常也更有效率。

4、一些用于表示数据的结构包含指向其他结构的指针。

实例

#include<stdio.h>
#defineLEN20

structnames//定义结构体names
{
charfirst[LEN];
charlast[LEN];
;

structguy//定义结构体guy
{
structnameshandle;
charfavfood[LEN];
charjob[LEN];
floatincome;
;

intmain(void)
{
structguyfellow[2]={
//这是一个结构嵌套,guy结构里嵌套了names结构
//初始化结构数组fellow,每个元素都是一个结构变量
{{"Ewen","Villard",
"girlledsalmon",
"personalitycoach",
68112.00
,
{{"Rodney","Swillbelly",
"tripe",
"tabloideditor",
432400.00

;


structguy*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->incomeis$%.2f:(*him).incomeis$%.2f\n",him->income,(*him).income);//68112.00
//指向下一个结构,him加1相当于him指向的地址加84。names结构占40个字节,favfood占20字节,handle占20字节,float占4个字节,所以地址会加84
him++;
printf("him->favfoodis%s:him->handle.lastis%s\n",him->favfood,him->handle.last);
//因为有了上面的him++,所以指向的是favfood1[1],
return0;





输出结果为
PSD:\Code\C\结构>cd"d:\Code\C\结构\";if($?){gccstructDemo02.c-ostructDemo02;if($?){.\structDemo02
address#1:000000000061FD70#2:000000000061FDC4
pointer#1:000000000061FD70#2:000000000061FDC4
him->incomeis$68112.00:(*him).incomeis$68112.00
him->favfoodistripe:him->handle.lastisSwillbelly
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。