我声明并定义一个函数,如下所示:
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;
}
最佳答案
我也在Windows上使用了gcc 4.4.3。
该程序成功编译并产生输出“ 11”:
#include <stdio.h>
unsigned int doSomething(unsigned int *x, int y);
int main()
{
unsigned int res = 0;
unsigned int x = 10;
res = doSomething(&x, 1);
printf("Result: %d\n", res);
return 0;
}
unsigned int doSomething(unsigned int *x, int y)
{
if(y)
{
printf("y is ok\n");
}
if(1)
{
printf("1 is ok\n");
}
return ((*x) + y);
}
请检查是否适合您,然后将其与您的程序进行比较。
确保正确声明了该函数。
关于c - unsigned int *代替int,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15064404/