C Advanced Cultivation Notes - Cultivation Notes 26: Essential Analysis of Pointers

--The difficulty of things is far less than the fear of things! 

    In this chapter, let's talk about pointers that make many people daunted. First of all, let's review the variables we talked about before. We know that variables in the program are just aliases for a piece of storage space , so the question here is: Is it necessary to pass This variable can use this storage space?

    For the question raised above, let's first look at a piece of code. Let's first think about what the output will be after execution:

#include <stdio.h>

intmain()
{
    int i = 5;
    int* p = &i;
	
    printf("%d, 0x%p\n", i, p);
 
    *p = 10;
	
    printf("%d, 0x%p\n", i, p);
    
    return 0;
}

Let's take a look at the execution results below:


From the output results, the value of variable i has changed, but we have not operated on variable i in the code, which is the problem to be analyzed in this chapter.

To analyze this problem, first let's take a look at the meaning of the "*" symbol associated with pointers:

    - When a pointer is declared, the * sign indicates that the declared variable is a pointer

    - When the pointer is used, the * sign means to take the value in the memory space pointed to by the pointer

    The -* number is similar to a key, through which the memory can be opened and the value in the memory can be read

    - The pointer is essentially a variable , but the value stored in this variable is a memory address

With the above three points, let's add comments to the above code

#include <stdio.h>

intmain()
{
    int i = 5;
    int* p = &i; //Define a pointer p, initialize the pointer variable p with the address of the variable i
	
    printf("%d, 0x%p\n", i, p);
 
    *p = 10; //Get the memory space pointed to by the pointer p through *, and set the value of the memory space to 10
	
    printf("%d, 0x%p\n", i, p);
    
    return 0;
}

I believe that through the comments, you already have a preliminary understanding of the pointer and the * sign, then use a picture to summarize:


To better understand pointers, let's look at an interesting piece of code:

#include <stdio.h>

intmain()
{
    int i = 0;
    int* pI;
    char* pC;
    float* pF;
    
    pI = &i;
    *pI = 10;
    
    printf("0x%p, 0x%p, %d\n", pI, &i, i);
    printf("%d, %d, 0x%p\n", sizeof(int*), sizeof(pI), &pI); //The pointer is also a variable, so it will also have an address
    printf("%d, %d, 0x%p\n", sizeof(char*), sizeof(pC), &pC);
    printf("%d, %d, 0x%p\n", sizeof(float*), sizeof(pF), &pF);
    
    return 0;
}

Direct execution output:


Let's analyze one by one:

    For the first line of output, I believe everyone can understand, here we focus on analyzing the output of the next three lines:

Basically, the size of the space occupied by the pointer type, the size of the space occupied by the pointer variable, and the address of the pointer variable are output. We see that whether it is a pointer variable of (int*), (char*) or (float*), the memory space occupied by them is 4 bytes , because our usual development platform is a 32-bit platform, That is to say, only 4 bytes are needed to access all addresses in the memory. Here you can remember: the pointer variable under the 32-bit platform occupies 4 bytes of memory space ! If the development platform is 64-bit, then the pointer size is 8 bytes.

    Let's take a look at call by value and call by reference

    - Because pointers are variables, pointer parameters can be declared

    - When a function needs to change the value of the actual parameter , it needs to use the pointer parameter

    - The actual parameter value will be copied to the formal parameter when the function is called

    - Pointers are suitable for functions with complex data types as parameters

    Here's an example:

#include <stdio.h>

int swap(int* a, int* b) //The formal parameter here is a pointer variable, so the function can change the value of the variable in memory through the * sign
{
    int c = *a;
    
    *a = *b;
    
    *b = c;
}

intmain()
{
    int aa = 1;
    int bb = 2;
    
    printf("aa = %d, bb = %d\n", aa, bb);
    
    swap(&aa, &bb); //Note that what is passed into the function here is a copy of the actual parameter, that is, a copy of &aa and &bb is passed to the function
                       //The function internally exchanges the values ​​of aa and bb by operating the copies of &a and &b
    
    printf("aa = %d, bb = %d\n", aa, bb);
    
    return 0;
}

The execution result is as follows. This example is relatively simple and will not be explained too much:



    Now let's look at constants and pointers

        -const int* p; //p is variable, the content pointed to by p is immutable

        -int const* p; //p is variable, the content pointed to by p is immutable

        -int* const p; //p is immutable, the content pointed to by p is variable

        -const int* const p; //The contents pointed to by p and p are immutable

Tips: Count left and right

    Left number: const is on the left side of *, the immutable is the data pointed to by the pointer

    Right address: const is on the right of *, the immutable is the pointer itself


Summarize:

    -Pointer is a special kind of variable in C language

- The value held by the pointer is the address     of the memory

    - Any address content in memory can be modified through pointers


Guess you like

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