I. INTRODUCTION of pointers
Fundamentally, a pointer (pointer) is a variable (or data object) that has a value of memory address. Pointers have four aspects that need to be mastered, the type of pointer, the type that the pointer points to, the memory area that the pointer points to, and the memory area occupied by the pointer itself. Before explaining these four aspects, let's first understand the indirection and fetch address operators.
1. Indirect operator: * and fetch address operator:&
(1) Indirect operator: *
Used at the time of declaration, indicating that a pointer variable is declared, for example, the p of int *p represents a pointer variable to an integer data; * When followed by a pointer name or address, the value stored in the pointer to the address, for example a = *p, indicates that the data on the address pointed to by P is assigned a value.
(2) Take address operator:&
& followed by a variable name, the address of the variable, such as the result of an int a = 1,&a, is the address of a.
2. Type of pointer
The type of the pointer simply removes the pointer when it is declared, that is, the pointer type of the p of int *p is int *, which is a pointer to type int.
3. The type that the pointer points to
The type that the pointer points to determines what the compiler treats as content in the memory area, the type that is declared, such as the type int *p the pointer to.
4. The memory area that the pointer points to
The value of the pointer is the number itself stored, which is a memory area address, the number of bits depends on the number of computer bits, 32-bit is the pointer has a 32-bit integer, the pointer to the memory area of the pointer is the value of the memory represented by the address of the beginning, The length is the number of bytes of the type that the pointer points to (for example, sizeof (int) for the int type).
5. The memory area occupied by the pointer itself
The pointer itself is represented by an unsigned integer, but the pointer is not an integer type, that is, you cannot add and subtract between pointers, the addition and subtraction of pointers is different, so the pointer is actually a new type, not an integer type, and the memory size is sizeof (the type of the pointer). For example, sizeof (int *).
Second, the arithmetic operation of the pointer
Pointers can be added and reduced, except that the meaning of the addition and subtraction is not the same as the integral type plus minus. For example:
int a[] = {12345678 9 Ten }; int *p = a;p+ +;
In the above, the pointer P is initialized to the address of the first element of a to the array (which is said after the array-to-pointer relationship), and then the pointer p increases by one, and the compiler adds the value of the pointer p to sizeof (int), since the address is in bytes. So the address that P points to is incremented by four bytes from the address of the original variable A, in other words, p points to the second element of array A. Similarly, add a number and subtract one number, namely n*sizeof (int) and-n*sizeof (int). Note: * (p+1) and *p+1 are not the same, the former represents the value on the memory location of p+sizeof (int), which represents the value of P's memory address plus one.
Iii. Pointers and Arrays
The array we always use is essentially a pointer: the array name is the address of the first element of the array.
inta[Ten] = {0,1,2,3,4,5,6,7,8,9,Ten}; intb; intc = A;///the first element address of array a intE = a +1;///the second element address of array a intf = *a;///consistent with F = a[0]
Declares an array of type array[n], the array name arrays has a twofold meaning:
First, it represents the entire array, and its type is type[n];
Second, it is a constant pointer, the type of the pointer is type*, the pointer to a type, that is, the type of the array cell, the pointer to the memory area is the array number No. 0 unit, the pointer itself occupies a separate memory area, note that it and the number of units No. 0 occupy the memory area is different. The value of the pointer cannot be modified, that is, an expression like array++ is wrong. Array names in different expressions can play different roles. In the expression sizeof (array), array name arrays represent the array itself, so the sizeof function detects the size of the entire array.
Iv. Pointers and functions
Assuming you write a function that handles an array and returns the sum of all the elements in the array, the Declaration and invocation of the function should be:
/* int marbles[10 ] = {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };total = sum (marbles); /* declaration of the function */ int sum (int *array);
We can make the array name the address of the first element of the array, so the argument marbles is an address that stores the value of the int type, and it should be assigned to a pointer-form parameter, which is a pointer to int. Since you can use the pointer to represent the array name, you can also use the array name to represent the pointer, and the function declaration above can be: int sum (int array[]). Note: You can use an array name parameter instead of a pointer parameter only in a function prototype or function definition header.
During the operation of the function, the value can be returned by return, but with the introduction of a pointer, it is possible to directly modify the value pointed to by the pointer without return, which demonstrates the flexibility of the pointer.
v. Pointer type conversions
Let's take a look at the example of the type conversion of pointers:
float 1.23f ; float *b = &a; /// assigns the address of variable A to pointer b int *c = (int *) &a; /// converts the address coercion type of variable A to an int * type and assigns the pointer C
When we initialize a pointer, a pointer expression is given to it, in general the type of the pointer is the same as the type of the pointer expression, and the pointer is pointing to the same type as the pointer expression, but if there is a type inconsistency between the two, the type conversion must be enforced.
If there is a pointer p, we need to change its type and the type of the pointer to Tyep *type, then the syntax format is: (type *) p; So the result of coercion type conversion is a new pointer, the type of the new pointer is type *, and it points to a type, The address it points to is the address that the original pointer points to, and all the properties of the original pointer p are not modified.
Vi. examples
intP//This is a generic integer variable.int*p;//first, start with the *, so that P is a pointer, and then it is combined with an int, indicating that the pointer points to the type int. So p is a pointer that returns an integral type of data.intp[3];//first, starting at p, first with [], stating that P is an array, and then combined with int, that the elements in the array are integers, so p is an array of integer dataint*p[3];//first, starting from P, first with [], because its priority is higher than *, so p is an array, and then combined with the *, that the element in the array is a pointer type, and then combined with an int, indicating that the pointer is the type of the content pointed to the integer, so p is an array of pointers that return the integer data int(*p) [3];//first, starting from P, first with the *, that P is a pointer and then the [] combination (and "()" This step can be ignored, just to change the priority), indicating that the pointer to the content is an array, and then combined with int, the elements in the array is an integer type. So P is a pointer to an array that consists of integral types of dataint**p;//first, starting with the P, first with the *, said that P is a pointer, and then combined with the *, indicating that the pointer points to the element is a pointer, and then combined with an int, indicating that the pointer to the element is an integer data. Because two-level pointers and more advanced pointers are rarely used in complex types, So later more complex types we don't consider multi-level pointers, at most only one-level pointers.intPint);//from P, first with (), that P is a function, and then into () analysis, indicating that the function has an integer variable parameters, and then with the outer int, indicating that the function return value is an integer type dataInt (*p) (int);//starting at p, first with the pointer, stating that p is a pointer, and then combined with (), indicating that the pointer is pointing to a function, and then in the () with the int, that the function has an int parameter, and then the outermost int, the return type of the function is integer, so p is a pointer to a function that has an integer parameter and returns a type of integer
(partial turn from 51131141)
C Language pointer explanation