初识结构体与指针

<span style="font-size:24px;">#include<stdio.h>
#include<stdlib.h>
struct data
{
 int num ;
 float price;
 char str[6];
};

void main()
{
 struct data  *p;
 struct data ddd;
 printf("%d\n",sizeof(p));
 
 ddd.num = 1;
 ddd.price = 1.1;
 strcpy(ddd.str,'aa');

 printf("%d,%f,%s\n",ddd.num,ddd.price,ddd.str);
 p = &ddd;
 printf("%d,%f,%s\n",(*p).num,(*p).price,(*p).str);
 printf("%d,%f,%s\n",p->num,p->price,p->str);
 system("pause");
}</span>


这么一段简单的代码,郁闷了一下午。。。。显示出现异常,内存什么什么的。。。怎么看都觉得这段代码没有问题,结果问题是出现在字符串的赋值上面。


把‘aa’ 换成“aa”

输出结构体的成员,有三种输出方式,第一种是,ddd.num,第二种是通过指针(*p).num,第三种是p->num。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章