About: char(*p)[10], char p[10] and char *p[10] discussion

Intuitive understanding:

        1. char(*p)[10] is a pointer to an array.

        2. char p[10] is an array p.

        3. char *p[10] is an array of pointers, and the array elements are char* pointers.


Specific differences:

      1. A pointer to an array

         In char(*p)[10], p points to an array of 10 char size, the difference between p+1 and p is sizeof(char p[10]), p+1 has pointed to another same size array too.

      2. Array pointer

         In char p[10], p is also a pointer, but a constant pointer, pointing to the address of the first element of the array (not the first address of the array) , see the following code:

[cpp]  view plain copy  
 print ?
  1. #include <stdio.h>  
  2. intmain  ()  
  3. {  
  4. char (*p)[10] ;  
  5. char a[10];  
  6. p=a;  
  7. }  
why prompt

[root@localhost ~]# cc test.c
test.c: in function 'main':
test.c:6: warning: assignment from incompatible pointer type?

You just change p=a to p=&a That's it.
This is where the c language confuses people. Many books tell you that the array name is the address of the array, but in fact it is the address of the first element of the array, and the type is char *. And &a represents the address of the array, and the type is char (*)[10] . Since the address of the array above the value is the same as the value of the address of the first element of the array, everyone usually thinks that &a and a are the same, resulting in a misunderstanding.
So, the warning for p=a is that you assign the address of a char to a pointer to char (*)[10]. Of course, the result is correct, but with a caveat.

     3. Array of pointers

        char * p[10], represents an array, each element in the array is a pointer, where sizeof(p) should be 40 on a 32-bit machine, =10 * sizeof(char*), here is the length of the array , rather than the length of the pointer, it also means that it is an array, which is also different from the pointer to the pointer (char **p)


also:

[cpp]  view plain copy  
 print ?
  1. char *p;  
  2. char a[10];  
  3. p = a;  

Here p = a means that p points to the address of a[0], and p points to a char.

If it is changed to p = &a, it means that p points to the array a, and p points to an a[10] array at this time!

p 仅仅是指向一个元素,而&a是整个数组的地址(&a实际上是指向数组a的指针),一个元素是不能指向整个数组的,因为它们类型不一致,编译器会警告!!!

虽然编译时会提醒警告:不兼容的指针类型,但还是能运行,实际上这是不对的。


正确的代码应该是:

int main()
{


    char (*p)[12] ; // type of p is char (*)[12]
    char a[12]; // type of a is char


    char *p1 = "aaddffgg";
    strcpy( a,"hello,world");


    p=&a; // &a represents the address of the array a, the type is char (*)[12]


    printf("p=%s\n "
    "the second char in p is %c \n",*p,(*p)[1]);


    printf("%s \n",p);// Warning type error
    printf("%p \n",p);// OK print p The address of the pointer
    printf("%s \n",*p);// p is a pointer to an array, so use the *p dereference operator
    printf("%s \n",p1);// The reason is OK p1 points to the first address of the element of the string
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325986890&siteId=291194637