C language pointer (xiaokangxiaobai)

I am Xiaokang Xiaobai, an ordinary Java Xiaobai. Love interesting words, life and distance.
Personal blog: https://blog.csdn.net/weixin_45791445 If you
have any questions, please contact QQ: 1059320343 (remember to remark CSDN)
Personal WeChat public account : Xiaokang Xiaobai

Insert picture description here

The first thing to remember-everything is an address

text:

1. Memory

  1. Memory (Memory) is also called internal memory and main memory . Its function is to temporarily store the calculation data in the CPU. Our program must be loaded into the memory before it can run.
    Insert picture description here

  2. All data in the computer must be placed in memory, and different types of data occupy different bytes. For example, int occupies 4 bytes, and char occupies 1 byte. In order to access these data correctly, each byte must be numbered, just like house number and ID number. The number of each byte is unique, and a certain byte can be accurately found according to the number. We call the number of bytes in memory the address (Address) or pointer (Pointer)

  3. Insert picture description here

  4. Insert picture description here

2. Everything is an address

  1. What the CPU needs when accessing the memory is the address, not the variable name and function name! Variable names and function names are just a kind of mnemonic for addresses. When source files are compiled and linked into executable programs, they will be replaced with addresses. An important task of the compilation and linking process is to find the addresses corresponding to these names.

  2. Insert picture description here

  3. Variable names and function names provide convenience to us, allowing us to use easy-to-read and understand English strings in the process of writing code, instead of facing the binary address directly, the scenario is simply frustrating.

    It should be noted that although variable names, function names, string names, and array names are essentially the same, they are all address mnemonics, but in the process of writing code, we think that variable names represent the data itself , And the function name, string name and array name represent the first address of the code block or data block.

3. So what is a pointer?

  1. The address of the data in the memory is also called a pointer. If a variable stores a pointer to the data, we call it a pointer variable.

  2. In the C language, a variable is allowed to store a pointer. This variable is called a pointer variable. The value of a pointer variable is the address of a piece of data, such a piece of data can be an array, string, function, or another ordinary variable or pointer variable.

  3. In other words, you must bring * when defining pointer variables, and not when assigning pointer variables.
    Insert picture description here
    Insert picture description here

  4. In other words, the use of pointers is to obtain data indirectly, and the use of variable names is to obtain data directly. The former is more expensive than the latter.
    Insert picture description here

4. The meaning of pointers

The meaning of pointers is indirect access.

Due to the design of the CPU, in many cases a register is needed to store the memory address of a variable, because the memory address is stored in the register at this time.

So you can read and write the variable value directly through this register. This is indirect access. In the C language, another variable type was invented, the pointer variable, specifically used to store addresses.

5. Pointers and Arrays

  1. p[i] <=> *(p+i)

  2. In the array, pointer variables increase by 1, their addresses increase by 4, 8, and 1, which are exactly the length of their corresponding data type + (int, double, char type)

  3. Array is just a simple arrangement of array elements in memory. There is no start and end mark. You can’t use sizeof(p)/sizeof(int) when calculating the length of the array, because p is just a pointer to int, and the compiler doesn’t know. Whether it points to an integer or a series of integers (array), so sizeof(p) finds the number of bytes occupied by the pointer variable p itself, not the number of bytes occupied by the entire array.

    In other words, the number of elements in the entire array cannot be deduced based on the array pointer, and information such as where the array starts and ends. Unlike strings, the array itself does not have a specific end mark. If you don't know the length of the array, you cannot traverse the entire array.

6. * and &

  1. The puzzle about * and &
    Suppose there is a variable a of type int, and pa is a pointer to it. What do *&a and &*pa mean?

    &a can be understood as (&a), &a means to take the address of variable a (equivalent to pa), *(&a) means to take the data at this address (equivalent to pa) Note: there is an asterisk before pa, which is identified by csdn For the oblique character, it is not displayed). Going around and back to the origin, &a is still equivalent to a.

    &*pa can be understood as &(*pa), *pa means get the data pointed to by pa (equivalent to a), &(*pa) means the address of the data (equivalent to &a), so &*pa is equivalent to pa.

  2. In the grammar we have learned so far, the asterisk * mainly serves three purposes:

    • Represents multiplication, such as int a = 3, b = 5, c; c = a * b;, this is the easiest to understand.
    • Means to define a pointer variable to distinguish it from ordinary variables, for example, int a = 100; int *p = &a;.
    • Indicates that obtaining the data pointed to by the pointer is an indirect operation, such as int a, b, *p = &a; *p = 100; b = *p;.
      Insert picture description here
int a = 0;
int *p = &a; // 取地址,p指向了a
printf("%d\n", *p); // 取内容 *可以想成取内容符,间接运算符

Brothers, it is not easy for Xiaobai to write articles. I hope that you readers will not go whoring for nothing, like, comment, bookmark, and follow.

Insert picture description here

For the brothers who are prostitutes

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45791445/article/details/109021475