您好我有下面的代碼指針在C結構體變量
#include <stdio.h>
#include <conio.h>
typedef struct test
{
int a;
int b;
int c[10];
}tester;
typedef struct done
{
tester* t;
int nn;
}doner;
void main()
{
doner d;
d.t = (tester*)malloc(sizeof(d.t));
d.t->a = 10;
d.t->c[0] = 10;
printf("%d\n", d.t->a);
getch();
return;
}
我認爲聲明:
d.t = (tester*)malloc(sizeof(d.t));
不正確,它應該是:
d.t = (tester*)malloc(sizeof(tester));
但是當我運行這段代碼不會崩潰,請讓我這是爲什麼。
有什麼問題,每個bug都不能保證崩潰 – perreal 2012-07-18 09:51:20
1)conio.h是一個非標準的頭文件。 2)main()應該返回int。 3)不要投射malloc()的返回值。4)#include 5)'d.t = malloc(sizeof * d.t);' –
wildplasser
2012-07-18 09:57:54