What is the type of &a in the following code?
char a[100];
myfunc(&a)
Is this even valid code? gcc -Wall complains about missing prototype but will otherwise generate code as if myfunc(a) was written.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The type of
&ain that code ischar (*)[100], which means “pointer to array of 100 chars”.To correctly prototype
myfuncto take that argument, you would do it like so:or the completely equivalent:
Addendum:
In answer to the additional question in the comments:
Yes, you would use
(*pa)[0]orpa[0][0]withinmyfuncto access the first element of the array.No,
&a(and thuspa) contain the address of the array. They do not contain the address-of-an-address. It should be obvious that the address of an array and the address of its first element are the same – the only difference is the type. Thus(void *)&a == (void *)ais true, and(void *)pa == (void *)pa[0]is also true, even if this seems a little unintuitive.Consider these two declarations:
Now, even though
pa[0][0]andppc[0][0]are both of typechar, the types ofpaandppcare not equivalent. In the first case, the intermediate expressionpa[0]has typechar [100], which then evaluates to a pointer to the first element in that array, of typechar *. In the second case, the intermediate expressionppc[0]is already achar *.