About array pointers, pointer arrays and secondary pointers in C language

concept explanation

Array pointer : First of all, it is a pointer, which points to an array, that is, a pointer to an array; in a 32-bit system, it always occupies 4 bytes. As for how many bytes the array it points to occupies, I don’t know. The array pointer points to a specific element in the array, not the entire array, so the type of the array pointer is related to the type of the array element.
Pointer array : First of all, it is an array, and the elements of the array are all pointers. How many bytes the array occupies is determined by the array itself. It is an abbreviation for "array storing pointers", that is, each element is a pointer.
Secondary pointers : If a pointer points to another pointer, we call it a secondary pointer, or a pointer to a pointer.

example explanation

Determine which is an array of pointers and which is an array pointer?

int *p1[10];
int (*p2)[10];

analyze

  1. "[]" has higher priority than " ". p1 is first combined with "[]" to form a definition of an array, the name of the array is p1, and int modifies the content of the array, that is, each element of the array. Therefore, this is an array that contains 10 pointers to int type data pointer, i.e. array of pointers
  2. “()” has a higher priority than “[]”, “*” and p2 constitute a pointer definition, the pointer variable name is p2, and int modifies the content of the array, that is, each element of the array. The array has no name here, it is an anonymous array. So p2 is a pointer, it points to an array containing 10 int type data, namely the array pointer
    memory layout

About the definition of p2

Usually when we define pointers, don't we always add the pointer variable name after the data type? Why isn't the definition of the pointer p2 defined according to this grammar? Maybe we should define p2 like this:
int (*)[10] p2;
int (*)[10] is a pointer type, and p2 is a pointer variable. This looks really good, but it just looks a little awkward. In fact, the prototype of the array pointer is indeed like this, but the pointer variable p2 is moved forward for convenience and beauty.

Use pointer to traverse array elements

#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
    int arr[] = { 1, 3, 5, 7, 9};
    int len = sizeof(arr) / sizeof(int);  //求数组长度
    int i;
    for(i=0; i<len; i++)
    {
        printf("%d  ", *(arr+i) );  //*(arr+i)等价于arr[i]
    }
    printf("\n");
    return 0;
} 
  1. (arr+i) is an expression, arr is the name of the array, pointing to the 0th element of the array, indicating the first address of the array, arr+i points to the i-th element of the array, (arr+i) means taking the i-th element data, which is equivalent to arr[i]. Among them, arr is a pointer of int* type, its own value will increase sizeof(int) every time it is added by 1, and its own value will be increased by sizeof(int) * i when adding i
  2. can also be expressed like this
 int arr[] = { 1, 3, 5, 7, 9};
 int *p = arr;

arr is the address of the 0th element of the array, so int *p = arr; can also be written as int *p = &arr[0];. That is to say, the three ways of writing arr, p, &arr[0] are equivalent, and they all point to the 0th element of the array, or point to the beginning of the array.

Traversing an array using array pointers

#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
    int arr[] = { 1, 3, 5, 7, 9};
    int len = sizeof(arr) / sizeof(int);  //求数组长度
    int i, *p = arr;
    for(i=0; i<len; i++)
    {
        printf(

Guess you like

Origin blog.csdn.net/u014265347/article/details/54882661