2

Possible Duplicate:
C pointer to array/array of pointers disambiguation

How is char (*p)[4]; different from char *p[4];?

Community
  • 1
  • 1
Mukul Shukla
  • 123
  • 2
  • 12

2 Answers2

8

http://cdecl.org/

  1. char (*p)[4]; -- declare p as pointer to array 4 of char.
  2. char *p[4]; -- declare p as array 4 of pointer to char.
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
4

char (*p)[4];: p is a pointer to a char array of length 4.

                         char [4]
    points to              |
     char [4]              v

   +------+             +------+------+------+------+
   |  p   |------------>|      |      |      |      |
   +------+             +------+------+------+------+
                         char    char   char   char  

   p will point to a char [4] array. Array is not created. 
   p is intended to be assigned at address of a char [4]  

char *p[4]; : p is an array of length 4, each location of the array is a pointer to char

              +------+------+------+------+
   p          |      |      |      |      |
an array      +------+------+------+------+
 itself          |      |      |      |
                 v      v      v      v  
               char*  char*  char*  char*

  p is an array and will be allocated in stack (if automatic)
phoxis
  • 60,131
  • 14
  • 81
  • 117