why does the printf not showing

Viewed 45

I need help. It doesnt display the printf in the mean function. I am doing a dynamic allocation in c and the add function works but the mean function does not display. There is no problem to the add function it works but the max does not. I am sorry I know this problem is simple but still cant get the answer. I am also getting a warning to the add function during the call in main.

This is my code:

typedef int* Statistician;

void add(Statistician answer, int *count, int *SIZE, int item);
                      |
                      [Note] expected 'Statistician' {aka 'int *'} but argument is of type 'int **'

int main() {
int SIZE;
Statistician *answer;
int count;
int item;

add(answer, count, SIZE, item);
    |
//[Warning] passing argument 1 of 'add' from incompatible pointer type [-Wincompatible-pointer-types]

printf("\nThe mean is: %.2f", mean(answer, SIZE));

return 0;
}

This is the add function:

void add(Statistician answer, int *count, int *SIZE, int item) {
int i;
printf("Enter n: ");
scanf("%d", &item);

answer = (int*)malloc(item * sizeof(int));

if(item == NULL) {
    printf("Memory not allocated.\n");
    exit(0);
}
else {
    for(i = 0; i < item; ++i) {
        scanf("%d", &answer[i]);
    }
    printf("Elements of array are: ");
    for(i = 0; i < item; i++) {
        printf("%d ", answer[i]);
    }
    
    if(item == 10) {
        int m;
        printf("\nAppend array: ");
        scanf("%d", &m);
            
        answer = realloc(answer, m * sizeof(int));
        
        for(i = item; i < item + m; i++) {
            scanf("%d", &answer[i]);
        }
        item = item + m;
        
        int temp, j;
        for(i = 0; i < item; i++) {
            for(j = 0; j <= i; j++) {
                if(*(answer + i) < *(answer + j)) {
                    temp = *(answer + i);
                    *(answer + i) = *(answer + j);
                    *(answer + j) = temp;
                }
            }
        }
        
        printf("Final array: \n");
        for(i = 0; i < item; ++i) { 
            printf("%d ", answer[i]);
        }
    }
}
}

This is the max function that doesnt display:

float mean(Statistician answer, int count) {
int mean =0;
int cnt = 0;

for(int i=0;i<count;i++){
    mean = mean + answer[i];
    cnt++;
}
mean = mean / cnt;
return mean;
}
1 Answers

I fully expected to find a duplicate but I didn't.

The first argument to add is declared to be int ** (via typedef) and you passed it a paramter of type int *. The compiler will let you do this with a warning, but it's almost always wrong. Don't do it.

If you're running 64 bit code, anything can happen after you stomp memory. 32 bit code is slightly more predictable but it's still going to end badly.

From your code, it looks like you want void add(Statistician *answer, Statistician answer; and add(&answer.

The deep learning of pointers is here. The thing you need to modify in the calling function is the thing whose address is passed to the called function. Allocating arrays with malloc almost always ends up being double pointers.

Related