C language: understanding of pointer array and array pointer

  • concept
    int p0[5];
    int *p1[5];		//指针数组
    int (*p2)[5];	//数组指针
    
    Pointer array: An array of pointers, representing an array, and each element of the array is a pointer type.
    Array pointer: a pointer to an array, representing a pointer, and a pointer to an array.
  1. Operator Precedence:
    ** ()> []> ***

  2. For example int p0[5], it is p0first []combined to form an array, the name of the array is called p0, and the intmodification is the content of the array, that is, each element in intthe array is of type, and the array contains 5 intdata variables pointing to integers.
    p0

  3. For int *p1[5]purposes because []priority higher than *, therefore, p1the first and []incorporated in and constitute array, the array name p1, but this time int *the modified content in the array, i.e., which contains five points within the array inttype data pointers , therefore, is Pointer array .p1

  4. For the int (*p2)[5]purposes, as ()having a higher priority [], so that the brackets ()in the *first and p2combinations thereof defining a pointer, the pointer variable called p2, but this time intmodified is each element in the array .
    In other words, it p2is a pointer that points to the first addressint of an array containing 5 types of data . Therefore, it is an array pointer . At this time, the array does not have an array name here, and is an anonymous array.

p2

  • Some concepts that are easy to confuse
  1. A plastic surgery int a;
  2. A pointer to an integer int *a;
  3. A pointer to a pointer that points to an integer int **a;
  4. An array with 5 integers int a[5];
  5. A pointer to an array of 5 pointers, the pointer points to an integer int *a[5];(array of pointers)
  6. A pointer to an array of 5 integers int (*a)[5]; (array pointer)
  7. A pointer to a function that has an integer parameter and returns an integer number int (*a)(int);
  8. An array with 5 pointers, the pointer points to a function, the function has an integer parameter and returns an integer int (*a[5])(int);
  • Memory description

    • Pointer array : First, it is an array . The elements of the array point to pointers, which means that the array stores pointers. The memory size (bytes) occupied by the array is determined by the array itself .
    • Array pointer : First of all, it is a pointer that points to the first address of an array, which means that it only stores an address that points to the array. In a 32-bit system, an address will always occupy only 4 bytes , and a 64-bit system will occupy 8 bytes.
  • Notes on array pointers

    int arr[5] = {
          
          1, 2, 3, 4, 5};
    char arr1[5] = {
          
          1, 2, 3, 4, 5};
    int (*p1)[5] = &arr;
    //错误示范——0
    int (*p2)[5] = arr;
    //错误示范——1
    int (*p3)[5] = arr1;
    
  • Error demonstration explanation:
    Because in C language, =the data on both sides must be the same . If they are different, explicit or implicit type conversion is required .
    The essence of an array pointer is a pointer , which stores the address , which points to the entire array , and &arr points to the first address of the entire array , arr is the address of the first element of the index group , so use caution.
    The second is because the intmodification in the array pointer is to point to the elements of the arr1array , and the elements in the array are all chartypes, and the types do not match, so it cannot be used in this way.

Guess you like

Origin blog.csdn.net/qq_30722795/article/details/106593075