C language pointer (play in minutes)

What is a pointer

To put it bluntly, the pointer is used to store the address of a variable
as shown in the figure:
Insert picture description here

(The editor is vc2010)

#include<stdio.h>
void main(){
    
    
	int a,*p;
	a=5;
	p=&a;
	printf("a=%d,p=%p,*p=",a,p,*p);
	getchar();

}

So through the situation just now, we found that we can not only get the value of a variable through the pointer, but also directly modify the value of the variable.

Another example is the following:

#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);
	getchar();

}

Array pointer

Speaking of pointers, what is the meaning of only basic int char float, the next is an array pointer to
combine the array and pointer. Of course, the array itself is passed by pointer when it is passed.
The following is a code demonstration

#include<stdio.h>
void main(){
    
    
	int a [5]={
    
    1,2,3,4,5},*p,i;
	p = a;
	for(i=0;i<5;i++){
    
    
		printf("&a[%d]=%p,a[%d]=%d\n",i,p,i,p[i]);
	
	}
	getchar();

}

Insert picture description here

We found that when p=a (pointer to array a), pointer p can be operated like array a, which is an array of pointers.

Array pointer

Since we talked about pointer arrays, we have to talk about array pointers. At first glance, the two names are very similar, but in fact the difference is still very large.
Int *p[]
is still taking the above example as an example.

#include<stdio.h>
void main(){
    
    
	int a [5]={
    
    1,2,3,4,5},*p[5],i;
	for(i=0;i<5;i++){
    
    
		p[i]=&a[i];
	}
	for(i=0;i<5;i++){
    
    
		printf("&a[%d]=%p,a[%d]=%d\n",i,p[i],i,*p[i]);
	
	}
	getchar();


}

We found that this stuff is actually an array of storage addresses.This memory address can be either int char or float and also an array. We will talk about this after we finish talking about the string array.

String array

There is no string class (java) and no str (python) in C language. There are only characters, so we introduced a string array to store strings.
('a' one byte "a" two bytes)
Pay attention here The thing is, a string array such as char a[5]="hello", on the surface there are only five characters, but in fact, there is a character at the end (behind'o') ​​in their memory distribution' \0' means the end, so only char a[5] can't fit hello, char a[6]="hello". So "a" is the two bytes of'a'+'\0' ('\ 0'do not display)

#include<stdio.h>
void main()
{
    
    
	char a[6]={
    
    'h','e','l','l','o'};
	//char a[6] ="hello";也行
	printf("%s",a);
	getchar();
}

In addition, please note that writing a "hello" directly is also an array of strings, but it is not named, we can use pointers to access

#include<stdio.h>
void main()
{
    
    
	char *p="hello";
	printf("%s\n",p); //%s 会读取第一个字符的地址知道读到'\0'
	printf("%c",p[1]);//输出字母e
	getchar();
}

Sao operation of array pointer

Look at a piece of code first

#include<stdio.h>
void main(){
    
    
	char *p[3]={
    
    
		"hello",
		"my",
		"friend"

	};
	printf("%s",p[1]);
	getchar();
	
}

Insert picture description here
Now we find that p[1] can be operated like a character pointer, which means that
p[1] is equivalent to

char a[4]="my",*c;
c=a;
where c is equivalent to p[1]

#include<stdio.h>
void main(){
    
    
	char *p[3]={
    
    
		"hello",
		"my",
		"friend"

	};
	int i;
	for(i=0;i<2;i++)
	{
    
    
		printf("%c\n",p[1][i]);
	}
	getchar();
}

Insert picture description here

Secondary pointer

This guy is a matryoshka, just look at a picture to understand
Insert picture description here

Function pointer

int (*p)( int x)
The function type pointed to by int
(*p) The pointer name
(int x) The parameters that need to be input to the function pointed to

This is a function,
for example

#include<stdio.h>


void main(){
    
    
	int go(int x);
	int x,(*p)(int x);
	scanf("%d",&x);
	p=go;//指针指向函数
	//(*p)(x)也行
	p(x);
	getchar();
	getchar();

}

int go(int x)
{
    
    
	printf("you input number is:%d",x);

}

What is the benefit of this stuff? With this stuff, I can directly pass another function to another function,
for example upgrade

#include<stdio.h>


void main(){
    
    

	int go(int x),x;
	void show(int x, int(*p)(int x));

	scanf("%d",&x);
	show(x,go);
	getchar();
	getchar();

}

int go(int x)
{
    
    
	printf("you input number is:%d",x);

}
void show(int x, int(*p)(int x)){
    
    
	printf("you have inputed!\n");
	p(x);

}

Insert picture description here

Pointer function

To put it bluntly, this is a function that returns an address such as:

int* go (int* x,int* y)

It looks like a function pointer, because * has a lower level of operation than (), so the function pointer adds (),
for example

#include<stdio.h>


void main(){
    
    
	
	int *go(int *x);
	int a=5,*p;
	p=go(&a);
	printf("a的地址为:%p,值为:%d",p,*p);
	getchar();


}

int *go(int *x){
    
    
	printf("地址以获取\n");
	return x;
}

Insert picture description here

File pointer

This is interesting, C language treats all devices as pointers.
FILE *fp=NULL;
fp = fopen("file path","w") ("w" is the open mode, w is written)
fprintf(fb, use "% s", str) this is the same stuff and print just in front of the pointer indicates
other words
printf ( "hello) and fprintf (stdout," hello "), like, stdout screen pointer, stdin keyboard pointer
empathy
fputs("hello",fp)

Next is to read the file
char s[100]
fp = fopen("file path","r")
fscanf(fp,"%s",s)
fgets(s,100,fp) (100 is the maximum length) The
difference is What is it, fprintf() reads a space or carriage return and returns, fgets() reads a carriage return and returns.
In addition, fread()
fread (receiving data address, how many bytes read, the number of data items, file Pointer)
other manuals are good.
Finally, don’t forget to close
fclose(fp)

Guess you like

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