1. What is a pointer:
When the program runs, any variables are stored in memory, by dividing the memory by the size of bytes, adding an index to each byte, and the address of a variable is the index of the first byte that the variable occupies in memory. At this point, a variable is defined to hold the index at the beginning of the first byte, which is called the pointer, and the variable that the pointer points to is indirectly accessed by the value of the variable. In a 32-bit system, pointers are 4 bytes in size and can be indexed up to 4G of memory.
2. Pointer definition
int Ten ; int *p = &a;
int b[3] = {1,2,4};
int *p = b;
3. Identification of common pointer types
//* The lowest priority, when the type is more complex, step-by-step analysis, you can also consider a part as a whole to simplify the analysisintp;int*p;//P is combined with *, stating that P is a pointer, and then combined with int, stating that p is a pointer to intintp[3];//p is combined with [], stating that P is an array, and then combined with int, that p is an array that holds an int variableint*p[3];//p is combined with [], stating that P is an array, and then combined with the *, indicating that the array contains a pointer, the pointer type is int, stating that p is a pointer to an array of int pointersint(*p) [3];//P is combined with a *, stating that P is a pointer, and then combined with [], indicating that p points to an array, which holds an intint**p;//p is a pointer to the INT pointer onlyintPint);//P is a function with an int and a return type of intint(*p) (int);//p is a pointer to a function with the function argument int and the return type intint* (*p (int))[3];//p is a function whose argument is int, and the return type is a pointer to an array with a pointer to an int in the number of times Group
4. Pointers Basic Concepts
Type of pointer: the type after which the pointer is removed is the type of the pointer, for example, the type of int *prt is int *
The type that the pointer points to: Remove the pointer name and the type after *, such as int *prt the type to which it points.
The value of the pointer: the memory that the pointer points to, and the value stored in the memory where the pointer variable resides, the pointer is a 32-bit variable in a 32-bit system that is treated as an address instead of a simple literal value
The address of the pointer: the address of the pointer itself in memory, the address is immutable
5. Pointer arithmetic
The pointer can be added to or subtracted from an integer, calculated as the size of the type pointed to by the pointer.
For example:
TypeA *intintint int1int ret = Addrb-addra;
The size of the RET is actually the size of sizeof (TYPEA), which is PTR + 1 o'clock, where the editor moves the sizeof (TYPEA) byte backward in the place where PTR points to the memory.
Analyze the results of the following prints:
Char a[" you_are_a_girl"; char *p = A; char **ptr = &p;printf ("**ptr=%c\n", **ptr); // Yptr++;p rintf ("**ptr=%c\n", **ptr); // Not defined
PTR is a level two pointer, *ptr is equivalent to p,p pointing to a[0], then **ptr = ' Y '
PTR is a level two pointer, *ptr points to P, that is, PTR holds the address of P, the PTR self-increment, equivalent to &p + sizeof (char *), does not point to any data in a, the behavior is undefined
6. Pointer expression
If the result of an expression is a pointer, the expression is called a pointer expression.
Cases:
Char*str[3] = { "hello,thisisasample!", "hi,goodmorning.", "Helloworld"}; chars[ the];strcpy (S, str[0]);//can also be written as strcpy (S,*STR);strcpy (S, str[1]);//can also be written as strcpy (s,* (str+1));
Pointer type conversions:
Different pointers can be cast, or they can be cast as an address to a pointer of a certain type. A type cast requires that the type that the source pointer points to is not smaller than the converted type, otherwise it may cause a cross-border.
structSt_type {intA; intb; intc;};structSt_type st = {5,4,3 };structSt_type *stptr = &St;int*p = (int*) stptr; (*P) =1; Re-assign a value to a* (P +1) =3; Re-assign a value to B* (P +2) =5; Re-assign the value to C
When used, it is possible that access through the above-mentioned means due to byte alignment is an error, the size of the byte alignment must be specified before use, for example:
#pragma // Save Alignment State #pragma Pack (4)// set to 4-byte aligned struct st_aaaa{ int A; int b; Char C; Long D; }; #pragma pack (POP)/ resume Alignment status
"C + + Common sense" C + + pointers