Ordinary fairy's learning record of pointer (from introduction to learning waste) -- Chapter 12

Keywords: C pointer

preface

The following is the explanation of the pointer from the definition, focusing on practice, with exquisite bushi small pictures. If you also find the pointer difficult, learn it with SThree~~

My skills are not high, so the explanation is very easy to understand~~

If you have any doubts or I am wrong, you are welcome to point out!!!

1, Definition and use of pointers

          A familiar saying is introduced: the pointer is the address of the variable. If you compare hotels, the variable is the guests, and the pointer is their room number.

         So pointer variables are variables that store variable addresses~

        How to define it? Let's take chestnut bell * * *:

               int  a;       int *point_a=&a;     

        * Indicates that a pointer variable is followed, point_ A is the name of the pointer variable. This sentence means point_ The address where the a variable is stored in a, i.e. point_ A points to a    (it's a's room number, hee hee) we can take the room number (point_a) and find the person in the corresponding room (* point_a=a) through the key (*)

        In addition, we call direct access to a (* point_a) and indirect access to a (* point_a) through the room number;

Exercise 1.1:

Observation results, I mean, point_a and & A are the same thing; * point_a and a are the same thing

#include<stdio.h>
int main()
{
	int a;
	int *point_a=&a;
	scanf("%d",point_a);

	printf("a=%d\n",a);
	printf("&a=%d\n",&a);
	printf("point_a=%d\n",point_a);
	printf("*point_a=%d\n",*point_a);
}

 

Exercise 1.2:

In the next exercise, let's feel the replacement of people in the room by keys.

#include<stdio.h>
int main()
{
	int i=10;
	int *p=&i;
	printf("*p=%d\n",*p);
	printf(" i=%d\n",i);
	*p=20;
	printf("*p=%d\n",*p);
	printf(" i=%d\n\n",i);
}

  Exercise 1.3:

Since we can exchange people through the key, let's try whether we can exchange the key for people in two rooms. (there's another explanation later, don't forget to have a look ~ ~)

#include<stdio.h>
int main()
{
	int *p1,*p2,*pp;
	int a=9,b=5;
	p1=&a;
	p2=&b;
	if(a>b)
	{
		pp=p1;
		p1=p2;
		p2=pp;
	}
	printf("a=%d,b=%d\n",a,b);
	printf("after:\na=%d,b=%d\n",*p1,*p2);
}
	

It can be seen that although the a and b values we set have not changed, the pointer to them has changed. (again, for example, I really like the chestnuts in this hotel)

In other words, people who exchange their keys and then open the door of the room through the key are different hhh (in this, we introduce the intermediate key as we used to exchange values, and leave a fake key to prevent being found)

 

2, Pointer as parameter of function:

         In the past, it was difficult for us to get two return values by calling a function (it seems impossible?), but with a pointer, we can do whatever we want.

        Then, if you want to get n values to be changed through function call, the method is as follows:

  1. Define n variables in main and point to them with n pointer variables respectively;
  2. The pointer variable is used as the argument, and the address of n variable is passed to the formal parameter;
  3. Through the formal parameter pointer variable, change the variable pointed to by the formal parameter pointer, that is, change the content of the address

Exercise 1.4:

Let's write a swap function to exchange two numbers. The same explanation will be later

#include<stdio.h>
void swap(int *pointer1,int *pointer2);
int main()
{
	int a,b;
	scanf("%d%d",&a,&b);
	printf("before : a=%d   b=%d\n",a,b);
	swap(&a,&b);
	printf("after  : a=%d   b=%d\n",a,b);
 } 

void swap(int *pointer1,int *pointer2)
{
	int t;//The variable t is used as a transfer station to exchange the contents pointed to by two pointers, so int t is used instead of int *t. 
	t=*pointer1;
	*pointer1=*pointer2;
	*pointer2=t;
 } 

In this problem, we change the content of the address through the formal parameter pointer variable.

Remember when we introduced a fake key in the last question, and we introduced a dummy in this question (a little afraid), because we can exchange keys in the same function, but the transfer between functions is value transfer. If we only exchange in the called function, it will not affect the main function.

However, we can know the address of the variable to be changed through the transfer of pointer variables, so exchanging the contents pointed by the pointer is equivalent to writing an article on the required variables.

The picture of this problem looks good. Ben baa is very satisfied~

Exercise 1.5:

The work of exchanging two numbers is over. We boldly take the next step. Can we create a function to obtain the maximum and minimum values of the array at the same time~   With a pointer, it's not difficult.

//The following code also writes many other functions. uu people can only focus on the main function and FindMM function;

//And the interface of this program is fried chicken friendly~

#include<stdio.h>
#define N 100 
void FindMM(int a[],int num,int *pMax,int *pMin);//Find the most important value in the array
void GetArray(int getn,int array[]);//Get array
int GetNumbers();//Get the number of input data
void PrintArray(int n,int array[]);//Print array

int main()
{
	
	int n,max,min;
	int num[N];
	printf("This program is made for finding the max and min.\n");
	
	n=GetNumbers();
	GetArray(n,num);
	PrintArray(n,num);
	int *pMax=&max;
	int *pMin=&min;
	FindMM(num,n,pMax,pMin);
	
	printf("the max number is %d\n\n",*pMax);
	printf("the min number is %d\n\n",*pMin);
	printf("byeeeeeee~~~~~~~~");
}


void FindMM(int *a,int num,int *pMax,int *pMin)
{
		*pMax=a[0];
		*pMin=a[0];
	int i,t;
	for(i=1;i<num;i++)
	{
		if(a[i]>*pMax)
		{
	        *pMax=a[i];
		}
			
		if(a[i]<*pMin)
	       *pMin=a[i];
	
	}
}

int GetNumbers()
{
	int number;
	printf("how many numbers do you want?(less than 100,please)\n");
	scanf("%d",&number);
	return number;
}	

void GetArray(int getn,int array[])
{
	int i;
	printf("you want %d numbers.now,please tell me the numbers.\n",getn);
	for(i=0;i<getn;i++)
	{
		printf("please give num[%d]=",i);
		scanf("%d",&array[i]);
	}
}

void PrintArray(int n,int array[])
{
	int j;
	for(j=0;j<n;j++)
	{
		printf("the %d number is %d \n",j+1,array[j]);
	}
	
}

 

  In fact, I think there are still many difficult points to understand about the problem of arrays, but the truth remains the same. Let's (● '◡ ●) talk about it in detail.

First, get the array (here I call many small functions, you can ignore this step and initialize the array directly). Then I set the variables max and min, and set pMax and pMin to point to them, which are passed to the formal parameters as parameters.

Then we know the addresses of max and min in the FindMM function. At this time, we assign the first element of the array to them (otherwise they may be little garbage).

then, compare, as usual, but because we use the addresses of max and min, we can change the variable value of the main function in the called function.

Next, let's take a look at pointers, arrays and functions. The application will be closer~~

2021.11.23

SThree Nan Yuzi

Posted by xtheonex on Tue, 23 Nov 2021 06:40:30 -0800