1. The type of the pointer and the type that the pointer points to
If you define a pointer int *ptr, the type of the pointer ptr is int*, and the type that the pointer points to is int.
Example 1:
int // pointer p type is int* int A; Char b;p=&a; // the pointer p-pointing type is int,a to int, which causes p to point to a*p=// that makes a=10
But the statement p=&b; Error, because in declaration statement int *p; Indicates that the type P refers to is int, and B is a char type, so the statement is not legal
2, * and & Understanding
& for the address operation, the result of the operation is a pointer, such as &A represents the address of a, such as a is an int, the result is an int *, can have an int *p=&a;
* is an indirect operation, such as *p means that p points to something, that is, p is the address of the storage unit, can have an int b=*p; (p to initialize, or p does not point to any address, is an indeterminate thing, the statement is not correct, here only to make a schematic omitted ...) )
Example 2:
intA= A; intb; int*p; int**ptr; P=&a;//The result of the &a is a pointer to the type int*, the type is int, and the address to a is the address of a. *p= -;//*p The result, where its type is int, and it occupies the address that P points to, it is clear that *p is the variable A. ptr=&p;//The result of the &p is a pointer, the type of the pointer is the type of P plus a *, here is the int**. The type that the pointer points to is the type of P, which is int*. The address that the pointer points to is the pointer P's own address. *ptr=&b;//*ptr is a pointer, and the result of &b is also a pointer, and the two pointers are of the same type as the one they are pointing to, so the amp;b to assign to *ptr is no problem. P re-point to B, i.e. p=&b;**ptr= the;//The result of the *ptr is what PTR points to, and here is a pointer to do another * operation on the pointer, and the result is a variable of type int. Even if you have to b=34;
3. The relationship between arrays and pointers
Declares an array of type array[n], the array name arrays has a twofold meaning:
First, it represents the entire array, which is of type [n];
Second, it is a pointer, the type of the pointer is type*, the pointer to the type is a type, that is, the array of cell types, 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. (While * (array+1) can, this is just the access element, and does not make array=array+1)
Access to an array can be done by means of array[i] or * (array+i), meaning the same.
Example 3
Char*str[3]={ "Hello,this is a sample!", "Hi,good morning.", "Hello World" }; 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)); strcpy (s,str[2]);//can also be written as strcpy (s,* (str+2));
In the above example, STR is an array of three cells, each of which is a pointer to a string. With the pointer array name STR as a pointer, it points to the No. 0 element of the array, which is of type char**, and it points to a type of char *.
*STR is also a pointer to the type char*, which is the type of char, and it points to the address of the string "Hello,this is a sample!" The address of the first character, which is the address of ' H '. Str+1 is also a pointer to the 1th element of the array, whose type is char**, which is the type of char *.
* (str+1) is also a pointer, its type is char*, it points to a type of char, which points to "Hi,good morning." The first character ' H ', and so on.
4. The relationship between the pointer and the structure type
Example 4
structMyStruct {intA; intb; intC; } mystruct SS={ -, -, +};//The struct object SS is declared, and the three members of the SS are initialized to 20,30 and 40. MyStruct *ptr=&ss;//a pointer to the struct object SS is declared. It is of the typemystruct*, the type it points to is mystruct.
int*pstr= (int*) &ss;//a pointer to the struct object SS is declared. But its type and the type it points to and PTR are different.
The more recommended way to access struct elements is:
ptr->a;
ptr->b;
ptr->c;
5. The relationship between pointers and functions
A pointer can be declared as a pointer to a function.
Example 5
int fun1 (char*,int); int (*PFUN1) (Char*,int); Pfun1=fun1; .... .... int a= (*pfun1) ("abcdefg",7); //
int a= fun1 ("ABCDEFG", 7); Commonly used function names call a function
Pointers to functions are seldom used, and the functions are called directly by function names, and the results are the same in both ways.
This paper summarizes his own understanding on the basis of reference to http://www.cnblogs.com/ggjucheng/archive/2011/12/13/2286391.html.
C + + pointer-related issues