c - Why can we use function pointers both as (*func_ptr)() and func_ptr() to invoke a function,but not so for array pointers? -
suppose have function pointer func_ptr
of type void (*func_ptr)()
.then know using can invoke function using pointer both :
(*func_ptr)(); func_ptr();
but again, suppose have pointer integer array int (*arr_ptr)[5]
, why can't refer array arr_ptr[]
, , consequently elements arr_ptr[0]
,arr_ptr[1]
etc? why can use (*arr_ptr)[0]
, (*arr_ptr)[1]
?
the type of arr_ptr[0]
int [5]
; type of (*arr_ptr)[0]
int
. if wanted to, use arr_ptr[0][0]
.
#include <stdio.h> int main(void) { int (*arr_ptr)[5]; int a[2][5] = {{1, 2, 3, 4, 5}, {11, 12, 13, 14, 15}}; arr_ptr = a; printf("%d %d\n", (*arr_ptr)[2], arr_ptr[1][2]); return 0; }
you can see code "running" @ ideone.
that function pointer can used either way (nice) sintactic sugar.
Comments
Post a Comment