Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用指针将某些内容传递给函数,然后在main中检索相同的指针?_C - Fatal编程技术网

C 如何使用指针将某些内容传递给函数,然后在main中检索相同的指针?

C 如何使用指针将某些内容传递给函数,然后在main中检索相同的指针?,c,C,我对一个非常简单的程序有问题。在WhatsBiger函数中使用b后,如何检索b的值?我知道我需要使用指针,但我不太明白。有人能试着编辑我的代码并留下相同的名字吗 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> int whatsbigger(int*, int*); int main() {

我对一个非常简单的程序有问题。在WhatsBiger函数中使用b后,如何检索b的值?我知道我需要使用指针,但我不太明白。有人能试着编辑我的代码并留下相同的名字吗

    #define     _CRT_SECURE_NO_WARNINGS
    #include    <stdio.h>
    #include    <string.h>

    int whatsbigger(int*, int*);

    int main()
    {
        int x = 10;
        int y = 25;
        int *b;
        int *biggest = b;
        int b = whatsbigger(&x, &y);

        printf("The biggest Value is: %d\n", biggest);
    }

    int whatsbigger(int *p, int *p2)
    {
        scanf("%d\n", b);
        int b;

        if (*p > *p2)
        {
            p = b;
        }
        else
        {
            p2 = b;
        }
        return 0;
    }
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
int whatsberger(int*,int*);
int main()
{
int x=10;
int y=25;
int*b;
int*max=b;
int b=什么更大(&x,&y);
printf(“最大值为:%d\n”,最大值);
}
int WhatsBiger(int*p,int*p2)
{
scanf(“%d\n”,b);
int b;
如果(*p>*p2)
{
p=b;
}
其他的
{
p2=b;
}
返回0;
}
#包括
int*whatsberger(int*,int*);
内部主(空){
int x=10;
int y=25;
int*maxist=whatsbiger(&x,&y);
printf(“最大值为:%d\n”,*最大值);
}
int*whatsberger(int*p,int*p2){
返回(*p>*p2)?p:p2;
}

如果您的目标是使用
scanf
为函数中的指针赋值,请执行以下操作:

int whatsbigger(int *p, int *p2)
{
    int b;
    scanf("%d", &b);
    if (*p > *p2)
    {
        *p = b;
    }
    else
    {
        *p2 = b;
    }
    return 0;
}

声明
intb必须在使用
b
之前。将
%d\n“
作为格式字符串保留到
scanf
将强制您在终端中提供新行,因此删除
\n
。如前一个答案所述,最好返回比较结果,以便
main
真正知道什么更大。

您有几个错误:WhatsBiger函数中的局部b偏差在哪里?为什么返回0而不是b?你应该打印*最大的而不是最大的<代码>返回b?我是不是遗漏了什么?
int whatsbigger(int *p, int *p2)
{
    int b;
    scanf("%d", &b);
    if (*p > *p2)
    {
        *p = b;
    }
    else
    {
        *p2 = b;
    }
    return 0;
}