The sixth part of pointers (array of pointers, pointers as function parameters) ---- 2021.3.6

Last talk about pointer links:

The fifth part of the pointer (const modified pointer, multi-level pointer) ---- 2021.2.22

Pointer array

When we touch pointers, we often come into contact with the two concepts of pointer arrays and array pointers. Let's first talk about the concept and usage of pointer arrays today. As for array pointers, we will mention them in a later article.

First of all, before talking about pointer arrays, I think it is necessary to introduce the concept of ordinary arrays, that is, integer arrays.

Integer array : is an array, each element of the array is an integer.
Then we can introduce pointer arrays from the concept of integer arrays.
Pointer array : is an array, each element of the array is a pointer.

Then under what conditions did we propose the pointer array?

Because as shown in the following code, we define three integer variables a, band c. And assign values ​​to 10, 20and respectively 30. Then we need to define three pointers to save the addresses of its three variables, namely *p1, *p2and *p3.

int main()
{
    
    
    int a = 10;
    int b = 20;
    int c = 30;
    int* p1 = &a;
    int* p2 = &b;
    int* p2 = &c;
}

We can do this normally, but what if we imagine that there are many parameters that need to save the address? Is it very trivial to use this method, then some people have proposed the concept of pointer array, which solves this problem very well. Let's see how to use it.

int main()
{
    
    
    int a = 10;
    int b = 20;
    int c = 30;

    //需求:  数组中的每一个元素都是指针(地址)
    int* num[3] = {
    
     &a,&b,&c };
    printf("%d\n",sizeof(num));

    for (int i = 0;i < sizeof(num) / sizeof(num[0]);i++)
    {
    
    
        printf("%d ", *num[i]);
    }
}

analyse as below:

  1. We define three integer variables a, band c. And assign values 10, 20and respectively 30. At this point we want to define a pointer array to store the addresses of these three integer variables, then we need to know what numthe data type of the pointer array is. For example, because athe data type of a variable is a inttype, it can be seen from the previous articles that &the data type of the variable will be added to the final data type, “ * ”and then the data type of the pointer array we need to define is the int *type.
  2. printf("%d\n",sizeof(num));This sentence actually numprints the byte size of the array type of the pointer array . The normal result is 12. Because we mentioned in the previous articles, no matter what data type pointer is, the final byte size is all 4. There are three variables of the same data type in the pointer array, so it is normal 3 x 4 =12, and the result is as follows:
    Insert picture description here
  3. So if we want to print element array of pointers that we first need to know there are several elements in the array of pointers, the sizeof(num)byte array of pointers to the total size of the elements, and the data type of the elements of the array of pointers are the same , Then we only need to extract a certain element in the array. Here, take the first element as an example, that is sizeof(num[0]), the size of the array element can be obtained by dividing the two formulas. In this example, is 3.
    Then finally print the pointer content, we need to “ * ”operate.
  4. In order to let everyone better understand, I specially drew a picture for everyone to watch.

Insert picture description here
So at this time we want to define a pointer to store the address of the first element of the array num, how should we define it?

This will use the multi-level pointers we mentioned in the previous articles.

For example, if num[0]the data type is a int*type, then we need to define a pointer variable to store its address. Then kthe data type of our newly defined pointer variable should be one level higher than the original “ * ”.

Code first! ! !

int main()
{
    
    
    int a = 10;
    int b = 20;
    int c = 30;

    //需求:  数组中的每一个元素都是指针(地址)
    int* num[3] = {
    
     &a,&b,&c };
    printf("%d\n", sizeof(num));

    int** k = num;
    for (int i = 0; i < sizeof(num) / sizeof(num[0]); i++)
    {
    
    
        printf("%d ", **(k + i));
    }
}

The analysis of the above code is as follows:

  1. From the previous analysis, we can see that the data type of our newly defined pointer variable k should be a int**type under normal circumstances , and our purpose is to save numthe address of the first element of the array , which is numequivalent to &num[0]. So the final definition isint** k = num;
  2. Then we want kto obtain the variable through the pointer variable a, band cthe content, that is 10, 20and 30. Then take athe content of the variable as an example. We need to derive level by level at this time. Because it kis a pointer variable, that is, the pointer variable kholds the numaddress of the first element of the array, that k = &num[0];is, we want kto get numthe content of the first element of the array through the pointer variable , that is, “ * ”operate on the pointer variable k . So even *k = &a;if you perform another “ * ”operation, you can extract the contents of the memory space of num[0]the corresponding variable pointed to by the pointer variable a, that is **k = 10;, you can finally kget athe contents of the variable through the secondary pointer . Achieved our goal. The other is the same.
  3. The final operation result is shown below.
    Insert picture description here
  4. At the same time, in order to let everyone better understand, I specially drew a picture for everyone to watch.
    Insert picture description here

Pointer as a formal parameter of a function

Code first! ! !

void swap(int x, int y)
{
    
    
    int  k = x;
    x = y;
    y = k;
    printf("x=%d y=%d\n", x, y);
}

int main()
{
    
    
    int  a = 10;
    int b = 20;
    swap(a,b);
      
    printf("a=%d b=%d\n", a, b);
    system("pause");
    return 0;
}

Analysis of the above code:

  1. We define the sum of two integer variables aand bassign the values ​​to the 10sum respectively 20. By then we eventually want to swapfunction aand bcarry out exchange value. That ais 20, bthe value of the final effect is , the value of 10. The results of the operation are as follows:
    Insert picture description here

  2. We can see from the above results that the final asum bvalue was not successfully exchanged. Why is this? In order to let everyone better understand, I specially drew a picture for everyone to watch.
    Insert picture description here
    Beginning avalue 10, ba value 20, then entering the swapfunction parameter xvalue 10, yvalue 20. So in this case defines an intermediate integer variable k, for temporarily storing xthe value, and finally through kto be xthe value assigned y, i.e. finally reaching xand yvalues are replaced by each effect, but when the exit swapafter the function, in fact, xand ythe equivalent Temporary variables will not be stored in virtual memory, that is, the final asum bvalue still has no real change, that ais 10, bthe final result is the same as the final result in the above figure, and the value is the value of 20. So how do we swaprealize the exchange of the values ​​of aand through functions b? At this time the pointer comes in handy!

  3. The modified swapcode is as follows:

void swap(int *x, int *y)
{
    
    
    int  k = *x;
    *x = *y;
    *y = k;
    printf("x=%d y=%d\n", *x, *y);
}

int main()
{
    
    
    int  a = 10;
    int b = 20;
    swap(&a,&b);
    
    printf("a=%d b=%d\n", a, b);
    system("pause");
    return 0;
}

Also in order to let everyone better understand, I specially drew a picture for everyone to watch.

Insert picture description here
The analysis of the above figure is as follows:
First of all, we can clearly see swapthat the interface parameter of the incoming function is the &asum &b, which is the address of the asum b. Then the pointer variable xand the pointer variable ypoint to the variable aand the variable respectively b. Then the exchange swapof the xsum in the function yactually directly manipulates the content of the variable asum bin disguise , that is, finally abecomes 20, bbecomes 10, successfully exchanged through the pointer, and the final operation result is as follows:
Insert picture description here

Concluding remarks

If you think this article is good, remember to like it and support it! ! !

Guess you like

Origin blog.csdn.net/qq_40544107/article/details/114267152