这个字符指针声明是什么意思?

时间:2012-04-29 14:42:21

标签: c pointers char

  

可能重复:
  C pointer to array/array of pointers disambiguation

char (*p)[4];char *p[4];的区别如何?

2 个答案:

答案 0 :(得分:8)

http://cdecl.org/

  1. char (*p)[4]; - 将p声明为char的数组4的指针。
  2. char *p[4]; - 将p声明为指向char的指针的数组4。

答案 1 :(得分:4)

char (*p)[4];p是指向长度为4的char数组的指针。

                         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是一个长度为4的数组,数组的每个位置都是指向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)