c - 使用指针的函数调用

标签 c function pointers compiler-errors

谁能解释一下为什么我会收到错误

cannot convert int ** to int* of argument1

我已经看过所有堆栈溢出的答案,但没有找到解决我的问题的方法。我的代码

#include<stdio.h>

int* sum(int,int*);

int main()
{
    int a=5;
    int *b;

    *b=6;
    int *res;

    res=sum(a,&b);
    printf("\n%d\n%d\n",a,*b);
    printf("%d",*res);
}

int* sum(int x,int *y)
{
    x=x+1;
    *y=*y+3;
    return (&y);
}

这是一个基本问题,但我发现很难解决该错误。

最佳答案

res=sum(a,&b);

这里 b 已经是一个指针(一个未分配的整数指针,可能会导致未定义的行为)。因此 &b 的类型为 int **。所以只传递b。

res(a,b);

之后返回 &y,它也是 int** 类型,如果要返回地址,请将其更改为 y(int * 类型)。

return y;

关于c - 使用指针的函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31109923/

相关文章:

c - 在C中反转文本文件

c++ - 函数计算被跳过的字符串类的字符?

类型为 "void(*)(int wall) is incompatible with parameter of type "int 的 C++ 参数”

c - 在C中获取数组的大小

c - 通过递增指针直到分配的内存末尾来将数据存储在 char 缓冲区中

C - float 字符串

c - 我什么时候释放变量decodeSet1?

c - 为什么对数组使用 `(char *[])` 而不是 `(char [][7])` 有效?

c# - 枚举函数错误

c - 函数除了它的数据成员(char 数组)之外不返回任何东西,可以在 main 函数中访问。这怎么可能?