Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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-我不知道';我不明白为什么这个代码是';t输出我选择的值。(指针)_C_Function_Pointers - Fatal编程技术网

C-我不知道';我不明白为什么这个代码是';t输出我选择的值。(指针)

C-我不知道';我不明白为什么这个代码是';t输出我选择的值。(指针),c,function,pointers,C,Function,Pointers,我应该接收并打印3个值,但每个部分都使用指针和函数。arrPointer是指向数组的指针,它指向getVar函数,从用户接收3个输入,将它们放入数组中,并将数组的地址返回给main。然后使用该地址打印3个变量。或者至少应该是这样。我做错了什么 #include <stdio.h> int getVar() { int *arrPointer; int values[3]; printf("Enter 3 values:\n"); scanf("%d

我应该接收并打印3个值,但每个部分都使用指针和函数。arrPointer是指向数组的指针,它指向getVar函数,从用户接收3个输入,将它们放入数组中,并将数组的地址返回给main。然后使用该地址打印3个变量。或者至少应该是这样。我做错了什么

#include <stdio.h>

int getVar()
{
    int *arrPointer;
    int values[3];

    printf("Enter 3 values:\n");
    scanf("%d", &values[0]);
    scanf("%d", &values[1]);
    scanf("%d", &values[2]);

    arrPointer = values;
    return(arrPointer);
}

void printFunc(int *arrPointer)
{
    int i;
    for(i = 0; i<3; i++)
    {
       printf("%d", *arrPointer);
        arrPointer++;

        }
    printf("\n");
}

int main()
{
    int *arrPointer;
    arrPointer = getVar();

    printf("The numbers entered in order are: ");
    printFunc(arrPointer);
}
#包括
int getVar()
{
int*arrPointer;
int值[3];
printf(“输入3个值:\n”);
scanf(“%d”,&value[0]);
scanf(“%d”,&value[1]);
scanf(“%d”,&value[2]);
arrPointer=值;
返回(arrPointer);
}
void printFunc(int*arrPointer)
{
int i;

对于(i=0;i您可以将指针设置为
getVar
堆栈框架中的数组地址

当该函数返回时,将弹出堆栈帧,这些值将超出范围(即,它们可以/不应该再被访问)

当您从
main
调用
printf
时,调用将重用先前由
getVar
使用的堆栈内存空间。同样,对于
printFunc

因此,无论他们使用它做什么,它都会覆盖/破坏由
getVar

有两种方法可以解决这一问题:

  • getVar
    执行
    malloc
  • main
    将数组的地址[在
    main
    的堆栈帧中]传递给
    getVar
  • 有关指针和数组的更详细解释,请参见我的回答:


    以下是带有
    malloc
    的版本:

    #include <stdio.h>
    #include <stdlib.h>
    
    int *
    getVar()
    {
        int *values = malloc(sizeof(int) * 3);
    
        printf("Enter 3 values:\n");
        scanf("%d", &values[0]);
        scanf("%d", &values[1]);
        scanf("%d", &values[2]);
    
        return values;
    }
    
    void
    printFunc(int *arrPointer)
    {
        int i;
    
        for (i = 0; i < 3; i++) {
            printf("%d", *arrPointer);
            arrPointer++;
    
        }
        printf("\n");
    }
    
    int
    main()
    {
        int *arrPointer;
    
        arrPointer = getVar();
    
        printf("The numbers entered in order are: ");
        printFunc(arrPointer);
    
        free(arrPointer);
    
        return 0;
    }
    

    更新:

    是否可以从函数返回一个数组或多个变量

    是的,在某种程度上,但是调用者必须为额外的值传递一个位置

    在上面的第二个示例中,另一种方式是调用方(即
    main
    )正在从
    getVar
    请求三个值,并在数组中为这些值提供空间

    另一种方法是调用方传递多个指针值,这些指针值显示返回值的位置:

    #include <stdio.h>
    
    void
    getVar(int *val,float *fval)
    {
    
        printf("Enter 2 values:\n");
        scanf("%d", val);
        scanf("%f", fval);
    }
    
    int
    main()
    {
        int val;
        float fval;
    
        getVar(&val,&fval);
    }
    

    请注意,我们还可以将
    malloc
    方法与
    struct
    结合使用。也就是说,
    getVar
    执行
    struct values*ret=malloc(sizeof(struct values));
    然后返回此指针(在填充结构之后).

    将指针设置为
    getVar
    堆栈框架中的数组地址

    当该函数返回时,将弹出堆栈帧,这些值将超出范围(即,它们可以/不应该再被访问)

    当您从
    main
    调用
    printf
    时,调用将重用先前由
    getVar
    使用的堆栈内存空间。同样,对于
    printFunc

    因此,无论他们使用它做什么,它都会覆盖/破坏由
    getVar

    有两种方法可以解决这一问题:

  • getVar
    执行
    malloc
  • main
    将数组的地址[在
    main
    的堆栈帧中]传递给
    getVar
  • 有关指针和数组的更详细解释,请参见我的回答:


    以下是带有
    malloc
    的版本:

    #include <stdio.h>
    #include <stdlib.h>
    
    int *
    getVar()
    {
        int *values = malloc(sizeof(int) * 3);
    
        printf("Enter 3 values:\n");
        scanf("%d", &values[0]);
        scanf("%d", &values[1]);
        scanf("%d", &values[2]);
    
        return values;
    }
    
    void
    printFunc(int *arrPointer)
    {
        int i;
    
        for (i = 0; i < 3; i++) {
            printf("%d", *arrPointer);
            arrPointer++;
    
        }
        printf("\n");
    }
    
    int
    main()
    {
        int *arrPointer;
    
        arrPointer = getVar();
    
        printf("The numbers entered in order are: ");
        printFunc(arrPointer);
    
        free(arrPointer);
    
        return 0;
    }
    

    更新:

    是否可以从函数返回一个数组或多个变量

    是的,在某种程度上,但是调用者必须为额外的值传递一个位置

    在上面的第二个示例中,另一种方式是调用方(即
    main
    )正在从
    getVar
    请求三个值,并在数组中为这些值提供空间

    另一种方法是调用方传递多个指针值,这些指针值显示返回值的位置:

    #include <stdio.h>
    
    void
    getVar(int *val,float *fval)
    {
    
        printf("Enter 2 values:\n");
        scanf("%d", val);
        scanf("%f", fval);
    }
    
    int
    main()
    {
        int val;
        float fval;
    
        getVar(&val,&fval);
    }
    

    请注意,我们还可以将
    malloc
    方法与
    struct
    结合使用。也就是说,
    getVar
    执行
    struct values*ret=malloc(sizeof(struct values));
    然后返回此指针(在填充结构之后)。

    有两个问题:

  • values[3]
    数组在
    getVar()
    函数中声明 函数结束时,分配给
    int值[3]
    超出范围。此问题的解决方案是分配 内存动态:
    int*值=(int*)malloc(sizeof(int)*3);
  • 函数
    getVar()
    的返回类型应为指针类型。将
    int getVar()
    更改为
    int*getVar()

  • 有两个问题:

  • values[3]
    数组在
    getVar()
    函数中声明 函数结束时,分配给
    int值[3]
    超出范围。此问题的解决方案是分配 内存动态:
    int*值=(int*)malloc(sizeof(int)*3);
  • 函数
    getVar()
    的返回类型应为指针类型。将
    int getVar()
    更改为
    int*getVar()

  • 要将值保存到数组中,您需要使用变量(int)将每个位置设置到数组中,就像您所做的那样。但不能使用arrPointer=values。 您还需要将指针传递给函数,而不是再次声明它们。 希望这对你有帮助并且有效

     #include <stdio.h>
    
    int getVar(int *values[])
    {
        printf("Enter 3 values:\n");
        scanf("%d", &values[0]);
        scanf("%d", &values[1]);
        scanf("%d", &values[2]);
    
        return values;
    }
    
    void printFunc(int *values[])
    {
        int i;
        for(i = 0; i<3; i++)
        {
           printf("%d", values[i]);
    
    
            }
        printf("\n");
    }
    
    int main()
    {
        int *arrPointer;
        int *values[3];
        getVar(values);
    
        printf("The numbers entered in order are: ");
        printFunc(values);
    }
    
    #包括
    int getVar(int*值[])
    {
    printf(“输入3个值:\n”);
    scanf(“%d”,&value[0]);
    scanf(“%d”,&value[1]);
    scanf(“%d”,&value[2]);
    返回值;
    }
    void printFunc(int*值[])
    {
    int i;
    
    对于(i=0;i要将值保存到数组中,您需要使用变量(int)将每个位置设置到数组中,就像您所做的那样。但是您不能使用arrPointer=value。 您还需要将指针传递给函数,而不是再次声明它们。 希望这对你有帮助并且有效

     #include <stdio.h>
    
    int getVar(int *values[])
    {
        printf("Enter 3 values:\n");
        scanf("%d", &values[0]);
        scanf("%d", &values[1]);
        scanf("%d", &values[2]);
    
        return values;
    }
    
    void printFunc(int *values[])
    {
        int i;
        for(i = 0; i<3; i++)
        {
           printf("%d", values[i]);
    
    
            }
        printf("\n");
    }
    
    int main()
    {
        int *arrPointer;
        int *values[3];
        getVar(values);
    
        printf("The numbers entered in order are: ");
        printFunc(values);
    }
    
    #包括
    int getVar(int*值[]