No, localarr
is not same as &localarr
localarr
is address of first element but &localarr
is address of array of chars of size 99. off-course values are same but both are different semantically.
localarr
is of type char [99]
whereas &localarr
is of type char (*)[99]
And because type of &localarr
is char(*)[99]
you should create pointer to array, like:
char(*ppl)[99] = &localarr;
where ppl
is ponter to array of chars of size 99
To visualize difference between localarr
and &localarr
below is my diagram:
+----+----+----+---+---+----+----+----+----+----+
| '1'| '2' |'3'|............... |98 | 99 | ...........
+----+----+----+---+---+----+----+----+----+---+----+
201 202 203 ............... 210 299
^ ^
| |
(localarr) (localarr + 1)
| |
|-----------------------------------------|--------
|201 | 299 // 299 -201 = 98
^ ^
| |
(&localarr) (&localarr + 1)
(Note:)
* localarr + 1
increment by one char size, where as (&localarr + 1)
increment by 99
array size.
have a look on this code and its output:
int main()
{
char localarr[99] = {'A'};
printf("%p %p\n",localarr, &localarr);
printf("%p %p\n",(localarr + 1), (&localarr + 1));
printf("%c %p %c\n",*localarr, *&localarr, **&localarr);
return 0;
}
Output:(read comments)
~$ ./a.out
0x7fff41ad64b0 0x7fff41ad64b0 // same
0x7fff41ad64b1 0x7fff41ad6513 // difference is 98, 4b1 + 62 = 513
A 0x7fff41ad64b0 A
Note: value of localarr
and &localarr
are same that is 0x7fff41ad64b0
But values of *localarr
and *&localarr
are not same:
*localarr
is first element
*&localarr
is address of first element **&localarr
is first element
that means *localarr
is same as **&localarr
that is first element localarr[0]
Note 0x7fff41ad6513 - 0x7fff41ad64b1 = 0x62
and 0x62 = 98 in decimal
What compiler error message is:
error: cannot convert ‘char (*)[99]’ to ‘char**’
^ ^ type of ppl
type of &localarr
Read this Issue with pointer to character array