Worship---pointer

I’ve heard that pointers are eternal gods, so let’s get in touch with them today~
Through pointers, the execution of some C programming tasks can be simplified, and some tasks, such as dynamic memory allocation, cannot be executed without pointers.

Pointer introduction

A pointer is a variable whose value is the address of another variable, that is, the direct address of a memory location.
Let's briefly understand the next pointer through an example:

#include <stdio.h>

int main()
{
    
    
	int  var1;
	char var2[10];

	printf("var1 变量的地址: %p\n", &var1);
	printf("var2 变量的地址: %p\n", &var2);

	return 0;
}

The results are as follows:

The address of var1 variable: 008FFAC4
The address of var2 variable: 008FFAB0

Use of pointers

The following operations are frequently performed when using a pointer: define a pointer variable, assign the variable address to the pointer, and access the value of the address available in the pointer variable. These are by using the unary operator * to return the value of the variable at the address specified by the operand.
For example, the following example:

#include <stdio.h>
 
int main ()
{
    
    
   int  var = 20;   /* 实际变量的声明 */
   int  *ip;        /* 指针变量的声明 */
 
   ip = &var;  /* 在指针变量中存储 var 的地址 */
 
   printf("Address of var variable: %p\n", &var  );
 
   /* 在指针变量中存储的地址 */
   printf("Address stored in ip variable: %p\n", ip );
 
   /* 使用指针访问值 */
   printf("Value of *ip variable: %d\n", *ip );
 
   return 0;
}

operation result:

Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20

Null pointer

In the variable declaration, if there is no exact address to assign, it is a good programming practice to assign a NULL value to the pointer variable. A pointer assigned a NULL value is called a null pointer.
The NULL pointer is a constant defined in the standard library with a value of zero.
On most operating systems, programs are not allowed to access the memory at address 0, because this memory is reserved by the operating system. However, the memory address 0 has a particularly important meaning, it indicates that the pointer does not point to an accessible memory location. But by convention, if the pointer contains a null value (zero value), it is assumed to point to nothing.

Pointer arithmetic operations

A pointer is an address represented by a value, so a pointer has 4 arithmetic operations: ++, -, +, -.

  • Each increment of the pointer points to the storage unit of the next element.
  • Each decrement of the pointer points to the storage unit of the previous element.
  • The number of bytes that the pointer jumps during increment and decrement depends on the length of the data type of the variable pointed to by the pointer, for example, int is 4 bytes.

Pointer increment operation

The following is a brief description of the arithmetic operation of the pointer with the increment operation of the pointer

#include <iostream>
using namespace std;

const int MAX = 3;

int main()
{
    
    
	int var[] = {
    
     10,100,200 };
	int i, * ptr;

	//指针中的数组地址
	ptr = var;
	for (i = 0; i < MAX; i++)
	{
    
    
		printf("存储地址:var[%d] = %x\n", i, ptr);
		printf("存储值:var[%d] = %d\n", i, *ptr);

		ptr++;//移动到下一个位置
	}
	return 0;
}

operation result:

Storage address: var[0] = 4c56f568
Storage value: var[0] = 10
Storage address: var[1] = 4c56f56c
Storage value: var[1] = 100
Storage address: var[2] = 4c56f570
Storage value: var[ 2] = 200

Therefore, the pointer decrement operation is the same operation.

Pointer comparison

Pointers can be compared with relational operators, such as ==, <and >. If p1 and p2 point to two related variables, such as different elements in the same array, then p1 and p2 can be compared.
E.g:

#include <iostream>
using namespace std;

const int MAX = 3;

int main()
{
    
    
	int var[] = {
    
     10,100,200 };
	int i = 0, * ptr;

	//指针中的数组地址
	ptr = var;
	while(ptr <= &var[MAX-1]) //如果变量指针所指向的地址小于或等于数组的最后一个元素的地址,则执行以下操作
	{
    
    
		printf("存储地址:var[%d] = %x\n", i, ptr);
		printf("存储值:var[%d] = %d\n", i, *ptr);

		ptr++;//移动到下一个位置
		i++;
	}
	return 0;
}

operation result:

Storage address: var[0] = 7370fb58
Storage value: var[0] = 10
Storage address: var[1] = 7370fb5c
Storage value: var[1] = 100
Storage address: var[2] = 7370fb60
Storage value: var[ 2] = 200

Pointer to pointer

Pointer to pointer is a form of multi-level indirect addressing, or a pointer chain. Usually, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, and the second pointer points to the location containing the actual value.

#include <stdio.h>

int main()
{
    
    
	int  var;
	int* ptr;
	int** pptr;

	var = 3000;

	/* 获取 var 的地址 */
	ptr = &var;

	/* 使用运算符 & 获取 ptr 的地址 */
	pptr = &ptr;

	/* 使用 pptr 获取值 */
	printf("Value of var = %d\n", var);
	printf("Value available at *ptr = %d\n", *ptr);
	printf("Value available at **pptr = %d\n", **pptr);

	return 0;
}

operation result:

Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000

Guess you like

Origin blog.csdn.net/weixin_44093867/article/details/107349612