#include <stdio.h>
void
foo (int (*ptr)[10]) {
(*ptr[0])++;
(*ptr[1])++;
(*ptr[4])++;
}
int
main(int argc, char **argv) {
int i;
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
foo(&arr);
for (i = 0; i < 10; i++) {
printf ("%d ", arr[i]);
}
printf ("\n");
return 0;
}
vm@ubuntu:~/src/tcpip_stack$ gcc test.c
vm@ubuntu:~/src/tcpip_stack$ ./a.out
2 2 3 4 5 6 7 8 9 10
*** stack smashing detected ***: terminated
Aborted (core dumped)
How to access the individual elements of the array through array pointer in foo ( ) ? Here it seems like, in foo() , based on array index, the entire array is being jumped as many times as index value.
void foo(int *A)and call from main:foo(a)...int[10]. Neither the function signature nor the call is incorrect (although non-idiomatic). It's just the dereferencing ofptrthat's wrong.void foo(int *A), foo(a)loses some type checking. OP'sfoo()expects and an array of 10int, not any size array likefoo(int *A)would accept.