最近我开始用c语言编写应用程序。我在下面的代码中遇到了sum运算符的问题。当我开始程序,我给予前。 4到int a;和6到int b;然后我想A + B巫婆给我22.这里是代码,首先,我定义我的结构:简单的结构与指针在c结构
struct student {
int a;
int *b;
}*s1;
然后我扫描的价值和其他步骤:
int sum, x, y;
s1 = malloc(sizeof(struct student));
scanf("%d", &x);
scanf("%d", &y);
s1->a = x;
s1->b = y;
sum = (s1->a) + (s1->b);
printf("SUM: %d", sum);
而且我测试* (s1-> a)仍然有问题。谢谢。
重要:我不想,我可以解决这个问题或建议编码算法的更好的方法的其他方式,我只是想知道为什么这段代码不工作正确,我该如何解决它。其实我想保留'b'作为指针!
int * b; < - 这是一个int指针,而不是一个int ... –
@MitchWheat所以我怎么能malloc b? –
erm,使用malloc !!! –