The difference between pointers and pointer variables (C language)

Tip: This chapter mainly talks about my understanding of C language pointers and pointer variables


foreword

Pointer is a very important concept in C language. Mastering the application of pointer can make the program more concise, compact and efficient. It can be said that not mastering pointers means not mastering the essence of C language.


提示:以下是本篇文章正文内容,下面案例可供参考

1. What is a pointer?

By learning C language, we know that when a variable is defined, the system will automatically allocate memory space for the variable. The compilation system allocates a certain length of space according to the variable type defined in the program.
Think about such a question:
How can I know where the memory space allocated by the system to variables is?

First imagine a real-life scenario:
when you go swimming in a swimming pool, you generally need a drawer to store your personal belongings, then you will get a key with the drawer number from the waiter, and then according to the key Find the corresponding drawer on the cabinet, and finally you can store the items in the drawer for safekeeping.

Through the above example, it is not difficult to find that
when we are looking for something, we can find it smoothly as long as we know its exact location. In the C language, each memory unit (drawer) in the memory area (cabinet) has an address (number), and C visualizes the address as a "pointer". It means that through it, the memory unit with its address can be found). The variable (key) contains the address value where the data is stored, and is used to access the data in the memory unit determined by the address.

C语言中,指针就是变量的地址。

// 定义一个整型变量a,赋值为3
int a = 3;
// &运算符获取变量a的地址
printf("%o",&a); 

insert image description here

2. What is a pointer variable?

一个变量的值是另一个变量的地址,且变量类型相同,则称该变量为指针变量。

// 定义一个整型变量a,赋值为3
int a = 3;
// 定义一个指针变量p,并初始化为NULL值
int* p = NULL;
// 将a的地址赋值给p
p = &a;

insert image description here
Note:

  1. When the pointer variable is defined, it will also be allocated memory space;
  2. *The sign indicates that the defined variable is a pointer variable;
  3. Changing the value of a pointer variable is actually changing the pointer;
  4. The type of the pointer variable must be consistent with the type of the variable pointed to.

3. Knowledge development

The first address of the storage space occupied by the variable in the memory is called the address of the variable; and the data stored in the storage space of the variable is the value of the variable.

野指针:
When the stack memory space pointed to by the pointer is released, the pointer pointing to it does not die. After the memory is released, the value of the pointer does not actually change. It still points to this memory, but the data stored in the memory becomes random. Value (garbled characters) only. The result of releasing the memory only changes the data stored in the memory, making the content stored in the memory garbage, and the pointer to the garbage memory is called a wild pointer.
After the memory is released, the pointer to it will not automatically become a null pointer, and a wild pointer is not a null pointer.


Summarize

To learn C language, you must first understand the difference between pointers and pointer variables, so as to avoid misunderstandings, which will be more conducive to the understanding and application of pointer operations and calculations.

Guess you like

Origin blog.csdn.net/weixin_48627356/article/details/123758920