int func (int a, int b, int *c);
int main()
{
int res, x, y, z; //int res, x, y, *z;
printf("enter x \n");
scanf("%i", &x);
printf("enter y \n");
scanf("%i", &y);
res = func(x, y, &z); //res = func(x, y, z);
printf("result addition: %i, result multiplication %i \n", res, *z);
return 0;
}
int func (int a, int b, int *c)
{
int result;
result = a +b;
*c = a*b; //(1) alternative:d=a*b
// c=&d
return result;
}
我的问题是,我不明白,为什么当我写它,因为我喜欢我的评论(这样做代码不起作用// )。编译器警告我,z没有初始化,但为什么在工作版本中没有这个问题?
另一件事我不明白的是,为什么它是正确的引用指针的值,就像我在(1),但它是错在下面的代码做了它:理解指针(指值)
int main()
{
int a, *c, *d;
scanf("%i", &a);
*c=a; //wrong
d=&a; //correct
return 0;
}
只是一个提示,但你应该总是把二进制运算符之间的空间,如乘法。你不想碰到一个bug,并且意识到你实际上已经解引用了某些东西,只是因为'*'被视为一元解引用操作符。你的代码很简单,所以你现在不必担心,但如果它变得复杂,你可能会犯错误。 – Kacy 2015-02-07 22:08:05
当我看到什么人投票下来,什么人投票在这里,它让我头痛 – Mcs 2015-02-07 22:11:16