Night5talker Night5talker - 1 year ago 34
C Question

C function and arrays

I'm learning C programming and passing arrays to multiple functions, not sure when and why my array is overwritten by something, help me debug and spot my mistake in code:

#include <stdio.h>

int insertNumbers(int *numbers, int howManny){
int i;

for(i=0; i< howManny; i++){
printf("Insert number:");
scanf("%d", &numbers[i]);
}
printf("\nNumbers :(insertNumbers function)\n");
for (int i = 0; i < howManny; ++i) {
printf("%d: %d\n",i, numbers[i]);
}
return *numbers;
}

int add(int *numbers, int howManny){
int sum = 0;
for (int i = 0; i < howManny; ++i) {
sum = sum + numbers[i];
}
return sum;
}

void printArray(int *numbers, int howManny){
printf("\nNumbers:(print array function)\n");
for (int i = 0; i < howManny; ++i) {
printf("%d: %d\n",i, numbers[i]);
}
}

int main(){
int numbers, howManny, sum = 0, numbersArray;
printf("How manny numbers do you want?");
scanf("%d", &howManny);

numbersArray = insertNumbers(&numbers, howManny);

sum = add(&numbers, howManny);
printf("Total sum is: %d",sum);

printArray( &numbersArray, howManny);


return 0;
}


result is

How manny numbers do you want?3
3
Insert number:10
10
Insert number:20
20
Insert number:30
30

Numbers :(insertNumbers function)
0: 10
1: 20
2: 30
Total sum is: 60
Numbers:(print array function)
0: 10
1: 3
2: 10


looks like my array is overwritten somewhere but not sure when and why

even when I try to use

printArray( &numbers, howManny);


still not working but getting 10, 10, 50 values

Vic Vic
Answer Source

Your array has not been created at the compile time. Also, the array size is given at the runtime of your program. Therefore, the array cannot be static so it has to be allocated dynamically and freed at the end, in order to work properly.

That being said, here I wrote a simple solution for what you're trying to achieve.

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

void insertNumbers(int *numbers, int howManny){
    int i;

    for(i=0; i< howManny; i++){
        printf("Insert number:");
        scanf("%d", &numbers[i]);
    }
    printf("\nNumbers :(insertNumbers function)\n");
    for (int i = 0; i < howManny; ++i) {
        printf("%d: %d\n",i, numbers[i]);
    }
}

int add(int *numbers, int howManny){
    int sum = 0;
    for (int i = 0; i < howManny; ++i) {
        sum = sum + numbers[i];
    }
    return sum;
}

void printArray(const int *numbers, int howManny){
    printf("\nNumbers:(print array function)\n");
    for (int i = 0; i < howManny; ++i) {
        printf("%d: %d\n",i, numbers[i]);
    }
}

int main(){

    int *numbers, howManny;

    printf("How manny numbers do you want?");
    scanf("%d", &howManny);

    // allocate memory (dynamic allocation)
    numbers = (int *)malloc(sizeof(int) * howManny);

    // validate memory allocation
    assert(numbers != NULL);

    // numbers is modified and returned by reference
    insertNumbers(numbers, howManny);

    printf("Total sum is: %d", add(numbers, howManny));
    printArray(numbers, howManny);

    // free dynamic allocation
    free(numbers);

    return 0;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download