Application of pointer

1, The concept of pointer

The pointer is actually a location where the address is stored. It is the same as the ordinary constant data defined by us. They only store different contents. Therefore, when using the pointer, we need to pay attention to the specific contents we store. In addition, the pointer can store the address of the pointer. This is double pointers, which is more troublesome.

2, Definition of pointer

The type pointed to by the pointer *Pointer name
int *p;//The pointer is called p. it is a pointer to integer data and stores the address of integer data
//Because the priority of the following two symbols has different effects, [] has a better priority than *, so they are combined first
int *p[3];//The pointer is called p array. It is a pointer to integer data and stores the address of integer data. The array is a pointer, so we call it pointer array, because all stored in this array are pointer variables, that is, some addresses
int (*p)[3];//This is the pointer to a two-dimensional array. For a two-dimensional array, its rows can be uncertain, but when storing elements, its columns must be fixed values, otherwise it can store data infinitely in one row, which is a one-dimensional array
int **p;;//This is a more complex pointer. I'm afraid it will become a pointer before the second * and then become a pointer when combined with the first *

Here, I want to emphasize the two-dimensional array. In fact, the two-dimensional array can regard it as multiple first addresses, which are arranged backward one by one, and then each first address is also arranged backward into the element. This is why the number of columns should be limited, otherwise the second first address cannot be determined, so the data cannot be stored

If we see that the pointer does not appear [], we can just look at *, and we can almost guess what type it is. It is more important to analyze the priority of operators layer by layer

The focus of pointer array and array pointer is in the front. Pointer array is a pointer, which stores all pointers (addresses). Array pointer is an array of words, and the first address of the array is stored with pointer

3, Simple use of pointer

1. Pointer access address

int a,*p=a;
//Note that the premise of our writing is that the variable to be addressed needs to be defined first. Only when it is defined, its address will be determined, and we can use the pointer for access
int *p,a;
p=&a;

*: this symbol indicates that it is a pointer

&: get address symbol

int *p,a;Pointers and ordinary data can be defined at the same time
p=&a;
//Here I want to emphasize that p is the address and * p is equivalent to a
scanf("%d",&a);
scanf("%d",p);
scanf("%d",&*p);

Example 1: single data

#include<stdio.h>
int main()
{
	int *p,a;
	p=&a;
	scanf("%d",p);
	printf("%d %d\n",a,*p);
	return 0;
 } 

Input: 3

Output: 3

p has stored the address of a, so they can now be said to be connected, so a and * p are the same when assigning values

Example 2: one dimensional array

Data inversion:

Writing method 1:

#include<stdio.h>
void dao(int *p,int *q,int n); 
int main()
{
	int a[100],b[100],n,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	dao(a,b,n);
	for(i=0;i<n;i++)
	printf("%3d",b[i]);
	return 0;
}
void dao(int *p,int *q,int n)
{
	int i,j;
	for(i=0,j=n-1;i<n&&j>=0;i++,j--)
	*(q+i)=*(p+j);
}

Method 2:

#include<stdio.h>
void dao(int *p,int n);
int main()
{
	int a[100],n,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	da1(a,n);
	for(i=0;i<n;i++)
	printf("%3d",a[i]);
	return 0;
}
void dao(int *p,int n)
{
	int i,j,t;
	for(i=0,j=n-1;i<=n/2&&j>=n/2;i++,j--)
	{
		t=p[i];
		p[i]=p[j];
		p[j]=t;
	}
}

Example 3: two dimensional array

Transfer of two-dimensional array

#include<stdio.h>
void pr(int (*p)[100],int n);
int main()
{
	int n,a[100][100],i,j;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	for(j=0;j<n;j++)
	scanf("%d",&a[i][j]);
	pr(a,n);
	return 0;
}
void pr(int (*p)[100],int n)
{
	int i,j;
	for(i=0;i<n;i++)
	{
	for(j=0;j<n;j++)
	printf("%3d",(*(p+i))[j]);
	printf("\n");
	}
}

The above is the pointer application of some integer data

In the one-dimensional string array, like the one-dimensional array, we mark its first position with a pointer, and then advance the rest backward in turn, so there is a writing method * p="I LOVE CHINA!", And he will actively add the last '\ 0'

The usage of two-dimensional string array is the same as that of ordinary two-dimensional array. The '\ 0' of each string of characters is also automatically supplemented

2. User defined function pointer

//Definition of function pointer variable
int (* Pointer name )(Points to the type of formal parameter in the function);

exemplars

Pointer to function

#include<stdio.h>
int max(int a,int b);
int main()
{
	int a,b,c;
	int (*p)(int ,int );
	p=max;
	scanf("%d %d",&a,&b);
	c=(*p)(a,b);
	printf("%d\n",c);
	return 0;
 } 
 int max(int a,int b)
 {
 	if(a>b)return a;
 	else return b;
 }
//Definition of pointer function
 type *Function name(Formal parameter table)
{
        Function body:
}
//Note that the return value here is each pointer, but after you use * it will be converted to a normal type

exemplars

Function pointer

Integer data

#include<stdio.h>
int main()
{
	int *max(int ,int );
	int a,b,c;
	scanf("%d %d",&a,&b);
	c=*max(a,b);//The function returns the address of a or b
	printf("%d",c);
	return 0;
}
int *max(int a,int b)
{
	if(a>b)return &a;
	else return &b; 
}

String type

#include<stdio.h>
int main() 
{
 	 char *day(int);
 	 char *p;
 	 int n;
 	 scanf("%d",&n);
 	 p=day(n);//We defined a pointer to store the address returned by the user-defined function. Note that I deleted the * in front of the user-defined function. If it is not deleted, the p at this time is a character, not an address
 	 printf("%s\n",p);
}
char *day(int n)
{
	static char *name[8]={"Error","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
	return ((n<1||n>7)?name[0]:name[n]);//The custom function returns the first address of a user-defined function
}

Note: it seems that the custom functions here are not declared in advance. My understanding is that they are described in the main function

Let's talk about pointer array and pointer of pointer. Pointer array has been discussed in the previous article. You can refer to it. I won't talk about it here

https://blog.csdn.net/weixin_61765266/article/details/122333631?spm=1001.2014.3001.5501

Pointer of pointer: to put it simply, a pointer points to a variable, but this point also has an address, so we use another pointer to point to this pointer

#include<stdio.h>
int main()
{
	char map[3][10]={"abc","def","ghi"},*a[3];
	int i;
	for(i=0;i<3;i++)
	a[i]=map[i];
	char **p;
	p=a;
	for(i=0;i<3;i++)
	printf("%s\n",*(p+i));//Output the contents of the variable a pointed to by p in turn. A contains an address, so a string of characters is directly output
	return 0;
}

I think this pointer means that a mantis catches cicadas and yellow finches behind. No matter what you are, it seems that there is always a pointer to this variable

Pay attention to the use of the * sign. If you are not careful, there will be more or less of this thing. Then the pointer is not pointing to a variable. It is amazing where it refers. This is also a difficult place for the pointer

The above is my study of the pointer. Some of it may be wrong. Welcome to leave a message and correct it

Keywords: C pointer

Added by Pedro Sim on Thu, 06 Jan 2022 00:44:38 +0200