【问题标题】:Array not storing value correctly in a for loop (C)数组未在 for 循环中正确存储值 (C)
【发布时间】:2021-05-27 13:53:48
【问题描述】:

我正在做一个学校项目。代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

float addNumbers(float *array){
    float t;
    int len = sizeof(array) / sizeof(array[0]);
    for(int i = 0; i < len;i++){
        t += array[i];
    }
    return t;
};

int main(){
    float n;
    float* v = (float*)malloc(10 * sizeof(float));
    int counter = 1;
    for(int i = 0;i < 4;i++){
        printf("Insert N%d: ", counter);
        counter++;
        scanf("%f", &n);
        v[i] = n;
        printf("%f\n", n);
    } 
    int len = sizeof(v) / sizeof(v[0]);
    for(int i = 0;i < len;i++){
        printf("%f", v[i]);
    }
    free(v);
    float x = addNumbers(v);
    printf("%f", x);
}

这段代码的输出是:

Insert N1: 5
5.000000
Insert N2: 7
7.000000
Insert N3: 8
8.000000
Insert N4: 3
3.000000

5.000000-1.#QNAN0

我发现问题在于数组如何存储值。插入 N$ 下方的值是 n 的值。 5.000000-1.#QNAN0 然而, 是存储在数组中的值,但它只有一个。我决定给数组更多的内存,但这不起作用。我知道的就这些了

【问题讨论】:

  • int len = sizeof(array) / sizeof(array[0]); 这是不正确的。这个函数AddNumbers 不能从float * 推导出大小。调用函数时必须提供大小。
  • len 是 10 - 无需使用 sizeof“计算”它。最好在开头添加#define LEN 10,并将malloc中的len10替换为LEN

标签: arrays c for-loop memory


【解决方案1】:

这里有很多问题。这是您的代码的注释版本:

float addNumbers(float *array){
    float t;       // t is uninitialized here.  It should be float t=0;
    int len = sizeof(array) / sizeof(array[0]);   // TERRIBLE see Note 1
    for(int i = 0; i < len;i++){
        t += array[i];
    }
    return t;
};

int main(){
    float n;
    float* v = (float*)malloc(10 * sizeof(float));   // do not cast malloc in C (Note 2)
    int counter = 1;
    for(int i = 0;i < 4;i++){
        printf("Insert N%d: ", counter);
        counter++;
       // better to test the result of scanf. Just try to input a alphabet (a or b)...
        scanf("%f", &n);
        v[i] = n;
        printf("%f\n", n);
    } 
    int len = sizeof(v) / sizeof(v[0]);  // Note 3
    for(int i = 0;i < len;i++){
        printf("%f", v[i]);
    }
    free(v);
    float x = addNumbers(v);    // You use a free-d array: Note 4
    printf("%f", x);
}

注意 1:在函数中,数组已衰减为指向其第一个元素的指针。 sizeof(array) 就是 sizeof(float *)。您必须通过使用的长度:

float addNumbers(float *array, int len){
    float t = 0.;
    for(int i = 0; i < len;i++){
        t += array[i];
    }
    return t;
};

注2:Do I cast the result of malloc?

注意 3:v 只是一个指向动态数组的指针。 sizeof(v) 就是 sizeof(float *)。假设你想使用上一次迭代的结束值,一种可能的方式是:

    int i;
    for(i = 0;i < 4;i++){
        ...
    } 
    int len = i;   // i is 4 at the end of the loop

注 4:释放后,动态数组已达到其生命周期,不能再使用(使用它会调用 Undefined Behaviour:green dragons live around...)。你应该写:

    float x = addNumbers(v, len);    // do not forget to pass the length...
    free(v);

【讨论】:

    【解决方案2】:

    您使用 malloc 为 10 个浮点数分配空间,然后设置前 4 个值,如前所述,尝试打印所有 10 个值,但正如 @Damien 所说,您不能将 sizeof(array) / sizeof(array[0]) 用于动态允许的数组。

    我建议你定义宏 LEN 并用它来驱动两个循环(如果你想要一个固定的大小):

    #define LEN 4
    
    ...
    
        float* v = malloc(LEN * sizeof(float));
        for(int i = 0; i < LEN;i++){
            printf("Insert N%d: ", i + 1);
            scanf("%f", v + i);
            printf("%f\n", v[i]);
        } 
    
        for(int i = 0; i < LEN; i++){
            printf("%f\n", v[i]);
        }
    
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2013-12-07
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多