C: What is the type of & array_name?

What is the type &ain the following code?

char a[100];
myfunc(&a)

Is this even valid code? gcc -Wallcomplains about the lack of a prototype, but otherwise generates code as if it were written myfunc(a).

+3
source share
1 answer

Type &ain this code char (*)[100], which means "pointer to an array of 100 characters."

In order for the prototype myfuncto accept this argument correctly , you will do this:

void myfunc(char (*pa)[100]);

or completely equivalent:

void myfunc(char pa[][100]);

Addendum:

In response to an additional question in the comments:

  • Yes, you must use (*pa)[0]or pa[0][0]internally myfuncto access the first element of the array.

  • , &a (, , pa) . . , - - . , (void *)&a == (void *)a , (void *)pa == (void *)pa[0] , .

:

char (*pa)[100];
char **ppc;

, pa[0][0] ppc[0][0] char, pa ppc . pa[0] char [100], char *. ppc[0] char *.

+15

Source: https://habr.com/ru/post/1724269/


All Articles