Pointer, pointer variable, dereference

What is the pointer

Pointer is address
We all know the memory of a computer. All programs in the computer are run in memory. Therefore, in order to effectively use the memory, the memory is divided into small memory units. In order to better access the memory units, the memory units are numbered. Here"Numbering" Just called address

Pointer variable

  • If there are variables, there will be space, variable name, and variable content. Then we need pointer variables to store the address of the variable. The pointer variable is also an ordinary variable, but the content stored in it is the address of the variable.

Pointer variables and their initialization

1. &a: Take address (unary operator): at this time, & means to go to the address of variable a

scanf("%d",&a)

2、 int *p=&a;This is to define a pointer variable At this time int *p defines a pointer p to int type,

int main()
{
    
    
	int a = 10;
	int*p = &a;
	printf("%d\n", a);
	printf("%p\n", &a);
	printf("%p\n", p);
	system("pause");
	return 0;
}

The running result is:

10
00D8FDCC
00D8FDCC

We can see that &a represents the address of a, and the pointer variable p represents the address of a

3、*p :Dereference : The value it refers to is the value of the variable pointed to by the pointer, not the address.

int main()
{
    
    
	int a = 10;
	int*p = &a;
	printf("%d\n", a);
	printf("%p\n", &a);
	printf("%p\n", p);
	printf("%d\n", *p);
	system("pause");
	return 0;
}

The running result is:

10//------->对应的a的值
00EFFA74//-------->对应的是a的地址
00EFFA74//--------->对应的是a的地址
10//-------->对应的a变量的内容
  • Multiple dereference
int main()
{
    
    
	int a = 10;
	int*p = &a;
	int* q = p;
	
	printf("p = %p\n", p);
	printf("*p = %d\n", *p);
	printf("q = %p\n", q);
	printf("*q = %d\n", *q);
	system("pause");
	return 0;
}

operation result

p = 008FFCA0
*p = 10
q = 008FFCA0
*q = 10
请按任意键继续. . .

It should be noted that p in int*p = a refers to the address of variable a; *p refers to the content of variable a.

Guess you like

Origin blog.csdn.net/supermanman_/article/details/109286880