Beginners of pointers in c language

Table of contents

1. What is a pointer

2. Pointers and pointer types

3. Wild Pointer

4. Pointer arithmetic



        1: What is a pointer

        In fact, the essence of the pointer is the address. In order to make better use of the memory space, we will divide the memory into cells one by one, and the unit of these cells is bytes, and the space of each byte All correspond to an address, these addresses are for us to better find the memory through the address, and then use the memory. For example, let's use specific examples to illustrate:

Under the 32-bit machine, we know that there are 32 high and low level lines, 1/0 (high level, low level), these 32 address lines can generate up to 2^32 addresses, and its size is 2^ The size of 32Byte is equal to 4GB, so under the 32-bit machine, our pointer can only manage 4GB of memory, under the 64-bit machine, the pointer can manage 8GB of memory, so the size of the pointer is 4 bytes, and 8 size in bytes.

2 pointers and pointer types:

1: pointer variable

        As the name suggests, the pointer variable is used to store pointers. It is an address. We can use the address of a number to find the content in the address. Let’s explain it through specific codes:>      

2: Pointer type: Let's explain it through the picture

I believe that many friends have such doubts. Since the size of the pointer is 4 bytes under the 32-bit machine, and the address in the memory can be managed, why do we need to distinguish the types?

If we have this question, we have to continue reading. Before that, we have to talk about the concept of wild pointers.

3: wild pointer

        On the web: wild pointers are where the pointer points to is not known (random, incorrect, without explicit bounds)

        As the name implies, the wild pointer means that the place pointed by the pointer is illegal, cannot be used, and is operated without permission.

        Let's introduce several situations below, which belong to the scope of wild pointers

                1: The pointer is not initialized

                Uninitialization will lead to random assignment of pointers, which will also cause pointers to randomly point to memory in memory, and when we use pointers, illegal access will occur, randomly using the space in our memory:    

 

2: Pointer out-of-bounds access:

        This is prone to bugs, especially when we use arrays, let's illustrate with an example:

 

3: The space pointed to by the pointer is freed:

 So how to avoid wild pointers?

1. Pointer initialization

2. Beware of pointer out of bounds

3. The pointer points to the space to release, and set NULL in time

4. Avoid returning the address of a local variable

5. Check the validity of the pointer before using it

When defining a pointer, we should think about where the pointer points to, and then be careful of crossing the boundary.

4: Pointer operations

        1 pointer + (-) integer operation

        It means that the pointer moves backward to the address of a data type. For example, if we are a pointer of int*, then we move a space of the size of an integer variable backward, and when we open up, we open up four bytes for a Space, then just skip an integer space to point to the next number, and we are using char* type pointers, we can only access 1 byte of space

 

2 pointer-pointer (when two pointers are used for operations, we can only perform subtraction operations)

and represent the elements contained between the two addresses

And the prerequisite for pointer-pointer is that the two pointers point to the same space, because it is unknown how many elements are different between the two, so the calculation result cannot be calculated.

        This article is over here, thank you for your patience.

 

        

Guess you like

Origin blog.csdn.net/2201_75964502/article/details/130713283