<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。