Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between (*p)[8] and *p[8] in C?

The two declarations are as follows:

int (*p)[8];
int *p[8];
like image 349
Hanafuda Avatar asked Dec 10 '22 08:12

Hanafuda


1 Answers

The first is a single pointer to an array of 8 integers, while the second is an array of 8 pointers, each to an integer.

If you just kick up cdecl, it's wonderful for this sort of thing:

pax$ cdecl
Type `help' or `?' for help

cdecl> explain int (*p)[8];
declare p as pointer to array 8 of int

cdecl> explain int *p[8];
declare p as array 8 of pointer to int

cdecl> explain char*(*fp[])(int,float*);
declare fp as array of pointer to function (int, pointer to float)
    returning pointer to char

There's actually a clockwise/spiral rule you can use to do this in your head but I haven't had to worry about that since I discovered cdecl, for the same reason I no longer convert large arbitrary 32 bit numbers from decimal to hex in my head any more - I can if I have to but it's so much easier with a tool :-)

like image 50
paxdiablo Avatar answered Dec 28 '22 18:12

paxdiablo