pointer superficial view

The pointer is the soul of the C language, it makes people cry without tears, makes people grit their teeth, but it is not invincible.

One: what is a pointer

        We can think of the computer's memory as a house, each house can store data and has a unique house number.

     

        int a=100;

        Since a is an integer variable, the computer opens up a four-byte space, lets a point to this space, and stores 100 in binary. This space belongs exclusively to a, so with the address of a, a can be found.

        As an entity, a pointer is a variable used to store a memory address.

Two: How to read pointer statements

        Let's go straight to:

int p;     defines an integer variable p  

int *p ;         defines a (wild) pointer

int * p[5];        Since [] has a higher priority than *, p is first combined with [], indicating that p is an array, and then combined with *, indicating that the element type of the array is int *, that is, the array p stores 5 A pointer, and finally combined with int, indicating that the data type of the content pointed to by the pointer is int, so p is an array composed of pointers to integer data

int (*p)[5];         Since *p has parentheses, p is first combined with *, indicating that p is a pointer, and then combined with [], indicating that the content pointed to by p is an array, and then combined with int, indicating that the array is The element type is int, so p is a pointer to an array of ints

int *(*p)[5];         Since *p has parentheses, p is first combined with *, indicating that p is a pointer, and then combined with [], indicating that the content pointed to by p is an array, and then combined with int *, indicating that The element type of the array is int*, so p is a pointer to an array of pointers

int (*p[5])[10];         Since [] has a higher priority than *, p is first combined with [], indicating that p is an array, and the array has 5 elements, we remove p[5], and the remaining The int (*)[10] is the element type, indicating that each element type in the array is an array pointer, and the content pointed to by each array pointer is an array composed of 10 elements, and the element type is int, so p is an array composed of array of array pointers to arrays

int * p();        Since () has a higher priority than *, p is first combined with (), indicating that p is a function, the parameter type of the function is void, and the return value type is a pointer, so p has a return value type no-argument function that is an integer pointer

int (*p)();        p is first combined with *, indicating that p is a pointer, pointing to a function, the parameter type of the function is void, the return value type is int, so p is a pointer to a return value type of int without parameters pointer to function

Three: Why do wild pointers appear?

1. The pointer variable is not initialized

int *a;
*a = 18;

Here we define a pointer variable a and assign 18 to the memory pointed to by a.

But we don't know where he's pointing and why?

Because we didn't initialize it, the location pointed to is random.

 Maybe A1, maybe E2, we don't know which house he went to and deposited 18, so the result is unknowable.

2. Out of variable scope

In the array, adding an array Array has ten elements, we can not use Array[10], because it is out of bounds, the essence of the array is a pointer, so when the pointer is out of bounds, the pointer cannot be used normally.

Four: Arithmetic operations on pointers

pointers and integers

We print out a, &a, &a[0], the address is the same

Explain that the array is a kind of pointer, and the array name is the first address of the array. When we perform arithmetic operations on the pointer, we actually move from one element of the array to another element.

For example, if we add +3 to the pointer, it will translate 3 elements to the right from the position it originally pointed to, which is the new address, of course, without crossing the bounds.

If you cross the border, everything is not easy to say~ 

おすすめ

転載: blog.csdn.net/m0_63742310/article/details/121973503