0

I'm working on a school project. Here's the code:

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

The output of this code is:

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

5.000000-1.#QNAN0

I discovered that the problem is how the array stores the values. The values below the Insert N$, are the values of n. 5.000000-1.#QNAN0, however, is the value stored inside the array, but it's only one. I decided to give the array more memory, but that doesn't work. That's all i know

  • 3
    `int len = sizeof(array) / sizeof(array[0]);` This is not correct. This function `AddNumbers` cannot deduce the size from a `float *`. You must provide the size when callling the function. – Damien Feb 25 '21 at 08:04
  • `len` is 10 - no need to "calculate" it with `sizeof`. Better add `#define LEN 10` in the beginning and replace `len` and `10` in `malloc` with `LEN`. – i486 Feb 25 '21 at 08:08

2 Answers2

0

You allocate space for 10 floats with malloc, then you set the first 4 values, and as noted earlier try to print all 10 but as @Damien said you cannot use sizeof(array) / sizeof(array[0]) for a dynamically allowed array.

I suggest you define macro LEN and use that to drive both loops (if you want a fixed size):

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

...
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
0

Many problems here. Here is a commented version of your code:

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);
}

Note 1: In the function, the array has decayed to a pointer to its first element. sizeof(array) is just sizeof(float *). You must pass the used length:

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

Note 2: Do I cast the result of malloc?

Note 3: v is only a pointer to a dynamic array. sizeof(v) is just sizeof(float *). Assuming you want to use the end value of the previous iteration, a possible way is:

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

Note 4: after free, the dynamic array has reached its end of life and can no longer be used (using it invokes Undefined Behaviour: green dragons live around...). You should write:

    float x = addNumbers(v, len);    // do not forget to pass the length...
    free(v);
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252