您好我有下面的代码指针在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