*** stack smashing detected ***: terminated

1.3k views Asked by At

I'm trying to print maximum value of array elements. Programm compiles fine, but when I input array values I get this message *** stack smashing detected ***: terminated. What I did wrong?

    #include <stdio.h>
int get_max(int ar[5]) {
        int i;
for (i=0;i<5;i++) {
        scanf("%d", &ar[i]);
}
int max = ar[0];
for (i=0;i<5;i++) {
    if (max < ar[i])
        max = ar[i];
    }
}
    return max;
}
int main() {
        int a;
        int k;
    k = get_max(&a);
    printf("%d",k);
    return 0;
}
1

There are 1 answers

0
Dave Costa On

You're allocating space for one int, passing the address of that to the function, and trying to treat it as an array of five ints.

If you want to pass an array, declare an array.

But really, there seems to be no point to declaring a in main() at all, and passing it to the function. Just declare the array locally in the function.