C Language Study Notes (Introduction 3) (Pointer)

What is a pointer

Here is an example to illustrate the power of pointers.

#include<stdio.h>
void main(){
    
    
	const int a=3;
	//这里定义一个const int 变量a 其作用就是锁定a的值使之不可变.
	//比如此时再让a=4就会报错
	a=4;

}

Insert picture description here
Then at this time, if you want to forcefully change the value of a, you must use a pointer.

#include<stdio.h>
void main(){
    
    
	const int a=3;
	int* prt;//创建一个指向int的指针
	//这里定义一个const int 变量a 其作用就是锁定a的值使之不可变.
	//比如此时再让a=4就会报错
	prt=&a;//取地址
	*prt=4;//提取所储存地址内的对应的值并修改为4
	printf("a=%d",a);

}

The results are as follows:
Insert picture description here

Memory diagram

For a better understanding, we can draw the following demonstration diagram;
Insert picture description here
let's take an example to see:

#include<stdio.h>
void main(){
    
    
	int a=3;
	int* prt;
	prt=&a;
	printf("a的值为%d\na的地址为%p\nprt存放的值为%p\nprt的内存地址为%p\nprt所存的地址对应的值为%d",a,&a,prt,&prt,*prt);

}

Insert picture description here

Pointer type

There is nothing to say about this. There are several data structures, and there can be several pointers. For a pointer is just a tool that points to the address of a certain variable. The advantage of this is that you can scope without creating a new variable. The influence of directly modifies a variable or takes a value, provided that the variable exists and the memory space is not destroyed.

int a ----> int* aprt
float b -----> float* b
char c -------> char* c
int arr[100] ----->int* arrprt[100]//数组

Pointer size

What is stored by the pointer is a hexadecimal memory address, which is essentially a value that can be added and subtracted for comparison.
Before explaining it, simply introduce the concept of an integer array. The name of the
integer array
array is just to use The stuff to store a set of data, to put it bluntly, is a list. It's just that in the C language, the integer list is stored in integers.

#include<stdio.h>
void main(){
    
    
	int* listprt[5];
	int list[5];//也可以这样定义不指明大小 list[]
	// 可以这样赋值 list[5]={1,2,3,4,5}也可以像下面一样
	int i;
	for(i=0;i<5;i++){
    
    
		list[i]=i;
		listprt[i]=&list[i];
	}//赋值
	
}

For an array, its memory distribution is as follows:
Insert picture description here

So when we want to display the value of the array, we can do this

#include<stdio.h>
void main(){
    
    
	int* x;
	int* listprt[5];
	int list[5];//也可以这样定义不指明大小 list[]
	// 可以这样赋值 list[5]={1,2,3,4,5}也可以像下面一样
	int i,j;
	x=&list;
	printf("list的内存地址%p,list的第一个元素的地址%p",x,listprt[0]);
	for(i=0;i<5;i++){
    
    
		list[i]=i;
		listprt[i]=&list[i];
	}//赋值
	for(j=0;j<5;j++){
    
    
		printf("list[i]=%d",*listprt[i]);
	}
	
	
}

Can also be like this

	
#include<stdio.h>
void main(){
    
    
	int* listprt[5];
	int list[5];//也可以这样定义不指明大小 list[]
	// 可以这样赋值 list[5]={1,2,3,4,5}也可以像下面一样
	int i,j;
	
	
	for(i=0;i<5;i++){
    
    
		list[i]=i;
		listprt[i]=&list[i];
	}//赋值
	printf("%p,%p\n",&list,listprt[0]);//补充前面说的
	for(j=0;j<5;j++){
    
    
		printf("list[i]=%d\n",*(listprt[0]+j));
	}

}

Here the pointer plus one or minus one refers to subtracting several memory units. Take the above example, assuming that int occupies two bytes, p is a pointer. Then, if p is 0x11111, then p++ is p+2 bytes and the result is 0x11113 In the same way, add 2 i.e. p+=2 and p is 0x11115

Secondary pointer

To put it bluntly, it is a nesting doll.
Just look at a piece of code below;

void main(){
    
    
	int a=4;
	int* p=&a;
	int**mp=&p;//二级指针,三级指针就是 int*** 
	printf("a=%d\np的地址为:%p\np指向的值为:%d,mp的地址是:%p,指向的是%p\na的地址是%p",a,&p,*p,&mp,*mp,&a);
}

The results are as follows: The
Insert picture description here
schematic diagram of the memory is as follows:
Insert picture description here
So combined with this figure, the three-level and four-pointers are the same. Infinite dolls.
In addition, pointers can also point to functions, that is, function pointers.
I'll talk about this in the next section.

Guess you like

Origin blog.csdn.net/FUTEROX/article/details/109476733