A preliminary understanding of pointers

1.0 Preface

Don’t be afraid of hearing pointers, pointers are not so difficult

1.1 & ----- Take address character

Foreword:
This student who learns C language may have been in contact with it a long time ago. When doing an input in scanf, it also emphasized that you must add'&', otherwise the program will crash. Now, let's understand this mysterious'&'.

Note: Every variable will have a certain value, and the value will occupy space, and the compiler will find an address to store the value in this variable.

Function: get the address of the variable, the operand can only be a variable
Note: the address of the variable is expressed in hexadecimal

Let's try it:

#include<stdio.h>

int main(void)
{
    
    
	int a = 10;
	printf("0x%x",&a);
}

A warning will be reported at this time:
Insert picture description here

The type of the parameter is unsigned int, but the &a given is of type int *.
We don't care about it, and we can see the result of the operation:

Insert picture description here

But why is there this warning? Just change %x to %p. %p is for printing the address. It is in hexadecimal format. It will be printed out, that is, how many digits are printed. The vacancy on the left will be filled with 0. So it can also be output correctly without adding 0x

#include<stdio.h>

int main(void)
{
    
    
	int a = 10;
	printf("%p",&a);
	return 0;
}

See, we also got the address of variable a: 0x22fe4c
Insert picture description here

1.2 Pointer

Preface:
Can we have the address of a variable so that we can access the value of that variable from the outside?

Use pointers.

  • The pointer is the variable that holds the address
  • The pointer can get an address and access the value of the corresponding variable

for example:

#include<stdio.h>

int main(void)
{
    
    
 int i = 10;
 int* p = &i;
 printf("&i的值为:%p\n",&i);
 printf("p的值是:%p",p);
 return 0;
}

Operation result: As
Insert picture description here
you can see, the address of i is the same as the value of p! In fact, they did the following:
Insert picture description here
Summary:

  • The value of the ordinary variable is the actual value
  • The value of the pointer variable is the address of the variable he points to

1.3 * operator

We have also talked about the'*' operator before, but we just use'* as a multiplication operator to calculate. The * mentioned here is a unary operator,
usage: * address
Function: take the value of the specified address, a tool
that can access the value through the address. So, the value obtained by the * operator is the value of the corresponding address, in i The stored value is 10, so let's try it with %d:

#include<stdio.h>

int main(void)
{
    
    
 int i = 10;
 int* p = &i;
 printf("*p的值为:%d\n",*p);
 printf("*&i的值是:%d",*&i);
 return 0;
}

Insert picture description here

  • The values ​​of *p and * &i are both the value of i, which is 10. In fact, this is easy to understand. The value of p is the address of i. Add * in front of p to get 10. As for *&i, &i gets the address of i first, and then adds * to get the value of i.
  • It can be temporarily understood that when the pointer has an * sign in front of it, it is an ordinary variable, and the value is the address of the variable it points to.

If we change the value of *p, will the value of the variable it points to also be changed? Let's try it.

#include<stdio.h>

int main(void)
{
    
    
	int a = 4;
	int b = 5;
	int *p = &a;
	int *q = &b;
	printf("a的值为:%d\n",a);
	printf("b的值为:%d\n",b);
	printf("这里试图用指针来改变a、b的值:\n");
	*p = 7;
	*q = 7;
	printf("改变后a的值为:%d\n",a);
	printf("改变后b的值为:%d\n",b);
	return 0;
} 

Insert picture description here
Let's try to change the variable value in other functions:

#include<stdio.h>
void f(int *a,int *b);
void q(int a,int b);
int main(void)
{
    
    
	int a = 4;
	int b = 5;
	printf("原本a、b的值是:%d,%d\n\n",a,b);
	f(&a,&b);//把a、b的地址传进f函数,在f函数里用改变后 ,在那个地址上的a、b也会发生改变 
	printf("被f函数用指针修改后的a、b的值是:%d,%d\n\n",a,b); 
	q(a,b);//把a、b的值传进q函数,只是复制一份a、b的值,所以main函数里的a、b不会改变 
	printf("被q函数修改后的a、b的值是:%d,%d\n\n",a,b);
	return 0;
} 
void f(int *p,int *q)
{
    
    
	printf("这里试图在f函数里用指针修改a、b的值;\n");
	*p = 7;
	*q = 7;
}
void q(int a,int b) 
{
    
    
	printf("这里试图在q函数里修改a、b的值;\n");
	a = 6;
	b = 6;
}

Insert picture description here

The values ​​of a and b have been changed! In other words:


  • The pointer can write to the variable it points to and change its value anywhere.
  • The variables in the function will not exist outside the function
  • Passing a and b directly into the function is just a copy of its value. There is no connection between the inside and outside of the function; but if the address is passed into the function parameter, it will receive itself.

1.4 Summary of pointer basic knowledge points

Insert picture description here

1.5 Application scenarios of pointers

Exchange the values ​​of two variables: For
example, make a function and pass the values ​​of two variables into the parameters. In that function, you can exchange them, but it is useless if you leave the function, because what you pass in is a copy The value over. Now that we have a pointer, we can pass in two addresses and use the addresses to change the value above.

If you want to change the value of a pointer in a function parameter, you can only pass the pointer of the pointer to the function.

#include<stdio.h>
void swap(int *pa,int *pb);
int main(void)
{
    
    
	int a = 4;
	int b = 5;
	swap(&a,&b);//把a和b的地址传进swap函数 
	printf("a=%d,b=%d\n",a,b);
	return 0;
}

void swap(int *pa,int *pb)
{
    
    
	int t = *pa;// *pa代表的变量是a,实际做的事是定义变量t,‘t = 4’。 
	*pa = *pb;// *pb代表的变量是b,实际做的事是‘a = 5 ’ 
	*pb = t;// t的值是4,实际上做的事是‘b = 5’ 
}

What it does is actually this:

int a = 4;
int b = 5;
int t;
t = a;//t = 4;
a = b;//a = 5;
b = t;//b = 4;

Guess you like

Origin blog.csdn.net/CSDN_C2/article/details/108548687