C 使用指针的函数调用

C 使用指针的函数调用,c,function,pointers,compiler-errors,C,Function,Pointers,Compiler Errors,谁能解释我为什么会出错 无法将参数1的int**转换为int* 我已经看到了所有堆栈溢出的答案,但没有找到解决问题的方法。我的代码 #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

谁能解释我为什么会出错

无法将参数1的int**转换为int*

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

#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);
}
#包括
整数*和(整数,整数*);
int main()
{
INTA=5;
int*b;
*b=6;
int*res;
res=总和(a和b);
printf(“\n%d\n%d\n”,a,*b);
printf(“%d”,*res);
}
整数*和(整数x,整数*y)
{
x=x+1;
*y=*y+3;
返回(&y);
}
这是一个基本问题,但我发现很难解决错误。

在您的情况下

 int* sum(int x,int *y)
函数接受一个
int
,一个
int*
,最后返回一个
int*
。现在,在函数定义中,您正在编写

return (&y);
在这里
y
int*
,因此
&y
产生
int**
,根据函数定义的返回类型[e],这是错误的

改变

 return (&y);

当您返回一个
int*
时,
y
已经是一个了

接下来,函数调用也存在同样的概念性问题。您不需要传递
res=sum(a,&b);
,只需要传递
b
,因为它已经是一个指针了

另外,请注意

  • main()
    的建议签名是
    intmain(void)
  • return
    不是函数。通常不需要在其表达式周围加括号,特别是在将变量名用作表达式时
  • res=总和(a和b)

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

    res(a,b);
    
    然后返回&y,它也是int**类型,如果要返回地址,请将其更改为y(int*类型)

    return y;
    

    因为
    y
    int*y
    (指针指向
    int
    ),所以
    &y
    int**
    (指针指向
    int*
    或指针指向
    int
    ),但您的函数(
    sum
    )必须返回
    int*
    ,而不是
    int**
    。您只需将
    return&y
    更改为
    returny
    即可解决此问题

    谁能解释我为什么会出错

    逐个检查您的代码:

    int a=5;       /* initializes a to value 5        */
    int *b;        /* declares pointer b              */
    
    *b=6;          /* OPS, segfault here              */
    int *res;      /* declares pointer res            */
    
    res=sum(a,&b); /* assigning to the address of res */
    
    语句
    *b=6;
    错误,您不应该这样做。您试图将值
    6
    分配给一个未动态分配的指针,但尚未将其设置为指向任何地址。

    您应该首先将指针指向变量的地址,例如:

    int c = 3;
    int *b = &c; /* create a pointer 'b' and make that
                    pointer point to the address of c */
    *b = 6; /* OK */
    
    请注意初始化
    int*b=&c;
    与声明然后分配
    b=&c
    之间的外观差异:

    int *b; /* declaring a pointer 'b'                 */
    b = &c; /* make that pointer point to address of c */
    
    接下来,创建指针变量“res”,并尝试将sum的返回值分配给res的地址:

    int *res;         /* declaring pointer 'res' */
    res = sum(a, &b); /* OPS, 'b' is a pointer   */
    
    查看函数“sum”,您试图使参数
    int*y
    指向
    b
    的地址,但使用指针的方法只是省略
    &
    ,因此只传递
    b
    sum(a,b);

    现在,
    sum
    的返回值为:

    int* sum(int x,int *y) /*
       ^ sum returns a pointer to int */
    
    根据您的代码,您希望将函数
    sum
    的结果分配给
    res
    的地址,因此您希望将
    y
    的地址分配给
    res
    的地址:

    res=sum(a,b); /* calling function sum */
    
    int* sum(int x,int *y) /* 'y' points to address of b */
    {
        x=x+1;
        *y=*y+3;
        return y; /* returning the address */
    }
    
    当sum返回时,
    res
    指向与
    y
    b
    相同的地址

    使用指针将地址分配给另一个指针的地址,例如:

    /* a and c are int's */
    int *x = &a;
    int *y = &c;
    x = &y; /* wrong */
    
    虽然这是正确的:

    int *x = &a;
    int *y = &c;
    x = y;  /* ok */
    

    我已经看到了stackoverflow的所有答案,但找不到解决我问题的方法
    …对不起,不知何故我不相信。:-)这就是你在编译和运行代码时遇到的错误吗?我不明白。不知何故,到目前为止,5个答案中没有一个提到这一点,但是
    int*b;*b=6;
    是一个错误,因为你通过ugh是一个未初始化的指针。在写入
    *b=6
    之前,必须将
    b
    指向某个地方(或者,将
    b
    更改为
    int b;
    b
    属于
    int*
    类型(正如Matt所说:在取消引用之前,需要对其进行初始化以避免UB)将
    &b
    传递给
    sum
    ,你传递了一个指向
    int*
    类型变量的指针,因此你传递了
    int**
    ,只需在call和return语句中松开
    &
    操作符,你就可以在指针中找到gosourav ghosh iam新手,我无法理解我在stac中找到的答案koverflow…….如果我的问题看起来很傻,我很抱歉:)谢谢sourav kanta:)…我是指针方面的新手…所以我对概念非常困惑..你能给我推荐一些网站,让我提高我的技能吗..谢谢:)@shweta:,这有点基本,但它试图解释并帮助防止主要的陷阱。指针真的不是这样吗如果你知道怎么做的话,很难,公平地说,指针可以像你希望的那样困难,但是基本的都很简单
    int *x = &a;
    int *y = &c;
    x = y;  /* ok */