我正在使用 mingw 编译器。该程序已成功编译,但在执行时,程序停止工作。请指出错误以及可能的调试方法。
#include<stdio.h>
#include<conio.h>
typedef struct
{
int a;
int b;
} two;
typedef union
{
int c;
float d;
} ad;
typedef struct
{
int a;
int b;
two l;
ad *n;
} one;
void trr(one *p);
int main()
{
one tr;
tr.a=3;
tr.b=3;
tr.l.a=5;
tr.l.b=6;
tr.n->d=5.43;
trr(&tr);
printf("a: %d\nb: %d\nl.a: %d\nl.b: %d\nunion: %f",tr.a,tr.b,tr.l.a,tr.l.b,tr.n->d);
return 0;
}
void trr(one *p)
{
p->a=(*p).a+1;
p->l.a=p->l.a*2;
}
最佳答案
typedef struct
{
int a;
int b;
two l;
ad *n;
} one;
....
tr.n->d=5.43;
n
成员是一个未初始化的指针。您需要使用 malloc()
为其分配内存或为其分配现有变量的地址。
如果您提高警告级别,编译器会警告您有关未初始化的变量。
关于c - 嵌套 union 和结构程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24402409/