int a[3]={ 1,2,3 };
char b[3]={'a ',' b ',' c ' 0 };
int * pi = a;
char * PC = b;
So at this time, both pi and pc actually store the computer memory address, which is actually a 4-byte number in a 32-bit computer, and these 4-byte numbers represent the specific address of a memory unit. therefore
If I want to visit a[0], I can do this. *pi is actually a visit to a[0]. If I want to visit a[ 1], I can do this: *(pi+ 1).
Similarly, for the character array b, you can access the value of b[0] through *pc and the value of b[ 1] through *(pc+ 1). Have you ever thought that the memory space occupied by integer data and character data is different? The number of bytes occupied by integer data is related to the word length of the machine. For example, on a 32-bit machine, integer data takes up 4 bytes, that is, 32 bits, while on the previous dos machine (that is, 16-bit machine), integer data takes up 2 bytes. For example, it takes 2 bytes to compile int under turbo C, 1 byte for char and 4 bytes for compiling int on VC++ 6.0.
Pi+ 1 points to the second element of the array. Similarly, pc+ 1 only considers the second element of array B. Although they all point to the second element of array, the number of bytes moved is different.
For example, in array B, b[0] and b[ 1] are two adjacent bytes, while in array A, a[0] and a[ 1] each occupy 4 bytes, so & A[0] and&A[ 1] are separated by 4 bytes.
Therefore, the number of bytes actually moved by pi++ and pc++ is different, which is discussed in detail in Tan Haoqiang's book.
Then a pointer consists of two parts, one is the pointer type, the other is what kind of data it points to, and the data type it points to is an important feature to distinguish different pointers.
int * p; And char * p1; P and p 1 are different pointers, one pointing to characters and the other to plastic data. When we use *p and *p 1 to access the memory cells they point to, the compiler will interpret them differently. One is to interpret the memory cells they point to as characters, and the other is to interpret the memory cells they point to as integer data. When we fetch data from memory, we will also decide how many bytes of data to fetch after the address pointed by the pointer according to the data type pointed by the pointer. In fact, these data are represented by binary 0 1 in memory. If you look at these data separately, they are the same and do not represent any special significance. They are all binary data, but the compiler will make different transformations when interpreting these data.
The data type pointed by the pointer determines its operation. For example, there are the following pointer variables:
int (*p)(int a,int b);
p = max
Here p is actually a pointer variable of a function, pointing to a function with two parameters of type int, and the function returns data of type int.
Then we can use this pointer to call the function int c = p (10,5);
In fact, there is also an address in the p variable at this time, and this address is the entrance address of the function. If p is a variable of type int *, the function call will not be like this.
In addition, we say that what is stored in the pointer variable is a pointer and an address. To put it bluntly, the address is a number used to mark the memory and also used to number the memory. We can cast:
char a = ' A
char * p = &a;
int * p 1 =(int *)p; //cast p to a pointer to integer data.
cout & lt& lt* p 1 & lt; & ltendl// output the ASCII code value of' a' 65.
I hope it helps you.