我声明并定义一个函数如下:
unsigned int doSomething(unsigned int *x, int y)
{
if(1) //works
if(y) //reports the error given below
//I use any one of the ifs above, and not both at a time
return ((*x) + y); //works fine when if(1) is used, not otherwise
}
我从 main() 调用函数如下:
unsigned int x = 10;
doSomething(&x, 1);
编译器报错和警告如下:
passing argument 1 of 'doSomething' makes pointer from integer without a cast [enabled by default]|
note: expected 'unsigned int *' but argument is of type 'int'|
我尝试对函数返回类型、函数调用以及参数类型使用所有可能的组合。我哪里错了?
完整代码:
unsigned int addTwo(unsigned int *x, int y)
{
if(y)
return ((*x) + y);
}
int main()
{
unsigned int operand = 10;
printf("%u", addTwo(&operand, 1));
return 0;
}