Viewing C Language Pointers from 5 Dimensions (Pointers are Just Paper Tigers)
Category Programming Techniques
In this article, I summarize the dimensions of pointers with four words: "Two Selfs and Three Others"! It may not be smooth or rhyming to read, what a thing. These "Two Selfs and Three Others", when expanded, are: Self Address, Self Value, Other's Value, Other's Address, and Other's Type.
I think we can talk about pointers from these five dimensions again. But before talking, I wrote a program that includes the "Two Selfs and Three Others" dimensions of pointers, and then explain the meaning of each dimension one by one, and you can see if it's the case.
In most scenarios of using pointers, these five dimensions should be enough to help you understand. However, in some special scenarios of using pointers, the 5-dimension method may not be able to help you.
Long article warning ahead, if you are impatient, you can bookmark this article and continue reading when you have time.
1. Program Code
1.1. Code
Example
#include <stdio.h>
int main(void)
{
int *pInt = NULL;
printf("The address of the pointer variable pInt itself is: 0X%x\n", &pInt);
//printf("The value of the pointer variable pInt itself is: 0X%x\n", pInt);
int para = 1;
printf("The address of the variable para itself is: 0X%x\n", ¶);
printf("The value of the variable para itself is: 0X%x\n", para);
pInt = ¶
printf("The value of the pointer variable pInt itself is: 0X%x\n", pInt);
printf("The other value of the pointer variable pInt is: 0X%x\n", *pInt);
int arr_int[2] = {1, 2};
pInt = arr_int;
printf("The address of the first element of arr_int[0] is: 0X%x\n", pInt);
printf("The address of the second element of arr_int[1] is: 0X%x\n", pInt + 1);
double *pDouble = NULL;
double arr_double[2] = {1.1, 2.2};
pDouble = arr_double;
printf("The address of the first element of arr_double[0] is: 0X%x\n", pDouble);
printf("The address of the second element of arr_double[1] is: 0X%x\n", pDouble + 1);
return 0;
}
The results of the run are as follows:
I like to use very simple programs to illustrate my own understanding when I talk about things in my own articles. So those who think that esoteric programs are needed to illustrate points, or those who despise low-level programs, please ignore me ^_^.
2.2. int type variable para
In the program:
int para = 1;
printf("The address of the variable para itself is: 0X%x\n", ¶);
printf("The value of the variable para itself is: 0X%x\n", para);
The variable para is defined, it has its own data value and its own storage address, which is very easy to understand. From the results of the run, the data value of the variable para itself is the hexadecimal "0X1", and the address is the hexadecimal "0X22feb4". In other words, in memory, the storage space starting at the address "0X22feb4" has 4 bytes storing a data value of "0X1". On my machine, an int type variable occupies 4 bytes, which may not be the same on your machine.
Everyone can easily understand the int type variable para. Next, I will draw a schematic diagram to indicate how the variable para is stored in memory, as follows:
Now let's start talking about the concept of "Two Selfs and Three Others".
Two Selfs and Three Others
"Two Selfs and Three Others", when expanded, are: Self Address, Self Value, Other's Value, Other's Address, and Other's Type.
2.1. Self Address
2.1.1 The concept of "Self Address"
"Self Address" is short for "one's own Now let's examine the difference between "pInt" and "pInt + 1", which is an operation using the "own value" of pInt. Looking at the results, the "own value" of pInt is "0X22feac", while the "own value" of pInt + 1 is "0X22feb0". Did you notice that they differ by exactly 4 bytes, and an "int" type of data also happens to occupy 4 bytes.
You might think that since pInt + 1 is adding 1 to the "own value", it should be "0X22feac + 1" = "0X22fead", right? Why isn't it like that? This is due to the "other type" of the pointer variable pInt.
In plain terms, "other type" means: "Hey pInt buddy, your other value is an int type of data value. In the future, if you use your own value to add 1, 2, or subtract 1, 2, don't be silly and really add 1 byte, 2 bytes, or subtract 1 byte, 2 bytes. The int type occupies 4 bytes, so you must add or subtract in units of 1 * 4 bytes, 2 * 4 bytes, or subtract 1 * 4 bytes, 2 * 4 bytes, got it? By the way, the N in pInt + N can be positive or negative."
Of course, if on your machine an "int" type of data occupies 8 bytes, then pInt + 1 is adding 8 bytes to the "own value", pInt + 2 is adding 8 * 2 = 16 bytes to the "own value", that's the idea.
I also provided an example in the program to illustrate this "other type". The program is as follows:
double *pDouble = NULL;
double arr_double[2] = {1.1, 2.2};
pDouble = arr_double;
printf("The address of the first element of arr_double is: 0X%x\n", pDouble);
printf("The address of the second element of arr_double is: 0X%x\n", pDouble + 1);
This time, a pointer variable pDouble is declared, its "other type" is a "double" type, its "own value" is the address of the array arr_double, and its "other value" is the data value of the element arr_double[0], which is "1.1". On my machine, a double type occupies 8 bytes, so pDouble + 1 is adding 1 * 8 bytes to pDouble's "own value", pDouble + 2 is adding 2 * 8 = 16 bytes to pDouble's "own value", pDouble - 1 is subtracting 1 * 8 bytes from pDouble's "own value", and pDouble - 2 is subtracting 2 * 8 = 16 bytes from pDouble's "own value". Wow! Friends can calculate for themselves whether the results are correct or not!
3. Summary
It's time to summarize.
I declare a pointer variable:
type *pType = NULL;
pType has five dimensions, which are:
pType = (own address, own value, other address, other value, other type);
3.1 Own Address: That is, "own address"
The pointer variable pType, as a variable, also has its own address. The common way to write code is "&pType".
Own address is not frequently used in general programs. If it is used, it involves "pointer to pointer", which is another topic and not discussed in this article;
3.2 Own Value: That is, "own data value"
The pointer variable pType, as a variable, also has its own data value. The code is written as "pType".
You can also perform addition and subtraction operations on the own value. Common code includes "pType + N", "pType - N", "pType2 - pType1", etc.
3.3 Other Address: That is, "other's address"
The own value of the pointer variable pType, in addition to representing its own data value, also represents the address of the "type" type variable bound with pType. Generally speaking, the own value and other address of the pointer variable pType are the same in terms of data value.
The common way to bind a type type variable with pType is: pType = &variable;
3.4 Other Value: That
Use the value or address of pChar1 to start from the address of the character 'a' and output the following 'b' and 'c' one by one.
The same understanding applies to pChar2 and pChar3.
4.2.2.2 Output each character of the first string "abc"
The code is as follows:
char *pArr = arr[0];
printf("%c ", *(pArr + index) );
The value of pChar1, which is arr[0], is given to the value of pArr, so both the value and address of pArr are the address of the character 'a'.
pArr + index is adding index * sizeof(char) bytes to the value of pArr, giving a temporary pointer variable pTemp:
char *pTemp = pArr + index;
The value or address of this pointer pTemp will be the addresses of the characters 'a', 'b', 'c' in turn, which means the value pointed to by pTemp will also be the characters 'a', 'b', 'c' in turn, so the pointer pTemp will traverse each character of the string "abc" in sequence.
4.3 Linked List
Linked lists are where pointers are used most frequently, and when inserting or deleting nodes, you will encounter the following code writing:
p2 = p1->next;
p1->next = p3->next;
......
What the hell is this? I'm so confused! This next pointer, that next pointer, jumping around, my mind is mush. Hehe, the 5-dimensional pointer analysis method is coming! However, regarding linked lists, I think it's better to open another article to talk about them. After the linked list is finished, I'll talk about these next pointers again. Let me tell you, the essence of the linked list is just like that, once you understand it, the linked list is even more of a paper tiger than the pointer.
>
Author: Shi Jia's Fish
Source: https://zhuanlan.zhihu.com/p/27974028