首页 文章

C - 我没有't understand why this code isn'输出我选择的值 . (指针)[重复]

提问于
浏览
0

这个问题在这里已有答案:

我应该接收并打印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);
}

3 回答

  • 1

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

    当该函数返回时,将弹出堆栈框架并且这些值超出范围 . (即他们可以/不应再访问) .

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

    因此,无论它们使用它,它都会覆盖/破坏由 getVar 创建的值 .

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

    • getVarmalloc

    • main 将数组的地址[在 main 的堆栈帧中]传递给 getVar

    有关指针和数组的更详细解释,请参阅我的答案:Issue implementing dynamic array of structures


    这是 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 中的堆栈变量的版本:

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

    UPDATE:

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

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

    在上面的第二个例子中,查看它的另一种方法是调用者(即 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);
    }
    

    但是,如果需要两个以上的值,则传递指针参数会变得乏味 .

    因此,在这种情况下,我们可以传递 struct 的地址,该地址可以包含任意数量的返回值:

    #include <stdio.h>
    
    struct values {
        int val;
        float fval;
        int count;
        int rval;
    };
    
    void
    getVar(struct values *ret)
    {
    
        printf("Enter 3 values:\n");
        scanf("%d", &ret->val);
        scanf("%f", &ret->fval);
        scanf("%d", &ret->count);
        ret->rval = rand() % ret->count;
    }
    
    int
    main()
    {
        struct values val;
    
        getVar(&val);
    }
    

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

  • 2

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

    #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);
    }
    
  • 0

    有两个问题:

    • values[3] 数组在 getVar() 函数中声明 . 函数结束后,为 int values[3] 分配的空间超出了范围 . 这个问题的解决方案是以动态方式分配内存: int* values = (int *)malloc(sizeof(int) * 3);

    • 函数 getVar() 的返回类型应为指针类型 . 将 int getVar() 更改为 int* getVar()

相关问题