C Language focus —— Pointer article ( An article that makes you fully understand the pointer )

one . preface

C Language is relatively low-level language , Why does he prefer the bottom , Because many of his operations are directly aimed at memory operations .

Let's explain this article C A major feature of language , It is also a difficulty , Pointer and pointer operation .

In this article, I will start with the basic types of stored procedures and principles , Then explain the pointer int *p, Draw inferences from one instance , Understand int **p and int ***p, Learn pointer .

Understand int *p,int **p and int ***p, Learn pointer completely !!!!

two . Understand the stored procedure and principle of a variable ( Must be clear )

2.1 direct '=' assignment
int a = 5; printf("a = %d",a);
result : a = 5

This sentence completes two operations , Let's first understand c What does language do inside a computer ?

Two operations :

(1)int a;

A variable is defined in the stack a, And opened up a in memory int Type size space , Namely 4 Bytes , Then let a Point to this space , That is, this space , The computer is assigned to a,
a After that, there will be a space of their own ;

(2) a = 5;

stay a Your own space , Store values inside 5 , hold 5 Convert to binary , Save to a of 4 Bytes of space .

2.2 Using input stream , Manual assignment
scanf("%d",&a);
We also have input assignment operations , It just proves the above point :

User entered a int Value of type , For example, input 5, then &a, Find it first a The address space of , Finally 5 Convert to binary , Deposit a The address space of , That is to say, the a Assignment of , That is, in a The piece of 4 The space of bytes is filled with binary 5;

2.3 summary

From the above, we can understand , Storage of a variable , First, open up a type size space from memory (int type 4 Byte size ), You're making variables point to this space , That is, this space belongs to this variable , Then store the values you want to store in this space .

three . Pointer type (int *) Stored procedure and principle of

3.1 Assignment specification of pointer type

(1) The first one is defined first and then assigned
int *p; p = &a; // This is the right way printf("p = %d\n",p);
result :p = 6618636

variable p Stored a Address

a key :

First understand , Pointer type ,int *p, Although it is *p Write together , But the variable is called p, Type is int
*, That is, the pointer type of integer , When you sort out the variable name and type , You already know more than half of the pointer .

(2) The second definition assignment is completed in one step
int *r = &a; printf("r = %d\n",r);
result :r = 6618636 Stored is a Address of

There is also a commonly used error assignment method :
// p = a; // This assignment is wrong
Wrong operation , You cannot assign a specific number to a pointer ( type mismatch ),

A pointer type , One int type

3.2 Pointer stored procedure and principle

Two common pointer assignment methods are listed above , Come down and explain what the computer has done ?

* You can store one int The address of a type variable is assigned to a int * Pointer type variable
* '=' On the left is a int * Pointer type variable , Can be stored int Address of type value
* '=' On the right is &a,a yes int Variable value of type 5,& Is the address character ,&a Just get int type a Address of the value
in general , Just put a The space of , Here it is p A key , Give Way p You can also be right a Operation in that space , This already belongs to pointer operation , We'll talk about it later .

It can be proved from the above ,c Assignment of language , Must be type corresponding

summary :int *p; Variable name p, Type is int *, Can store one int Address of data .

be careful : This can store one int Address of data , Not an address , yes int type

for example :
int a = 5; int *p; p = &a;
* here a It's a int Variable of type , Stored int Value of type 5
&a Taken to storage int type a Address of

p = &a; hold int type a Your address is assigned to int * Type p

That is int * A variable of type can hold one int Address of data

four . Pointer type (int **) Stored procedure and principle of
int **q; q = &p; printf("q = %d\n",q);
result :q = 6618624 Stored p Address of

int *p got it , that int **q And ?

first : First, separate the data types and variables

int **q; The variable name is q, Data type is int **

int * Stored in int Address of type data

int ** Stored in int Address of type data

We understand the above , One * Refers to a int Address of value ,

p Stored in is a int Address of value ,p = 6618636, by a Address of .

Then we can push to , Two *q It's storage p Address of .

p deposit a Address of ,p Itself is also a variable , His value is a Address of ,

And the memory also opened up a space for himself , Let him store his values

q deposit p Address of ,q Is also a variable ,

 

Their directional relationship is as follows :

a<----p<----q

five . Pointer type (int ***) Stored procedure and principle of
int ***m; m = &q; printf("m = %d\n",m);
result :m = 6618616 Stored q Address of

since ,int * and int ** I understand , that int *** It's easy to solve

Similarly ,int*** Stored is int ** Address of type data

six . Pointer operation (* operation )

This is what we call the pointer's own operation
printf("p = %d\n",p); printf("*p = %d\n",*p); printf("q = %d\n",q);
printf("*q = %d\n",*q); printf("**q = %d\n",**q); printf("m = %d\n",m);
printf("*m = %d\n",*m); printf("**m = %d\n",**m); printf("***m = %d\n",***m);
result :
p = 6618636 *p = 5 q = 6618624 *q = 6618636 **q = 5 m = 6618616 *m = 6618624
**m = 6618636 ***m = 5
p,q and m Are the variables in the above example

First, except when defining pointer variables , Variables are preceded by * Define type for , Other times are pointer value operations , Note: value , Not addressing , Take the value stored in the pointer variable .

6.1 Take a real chestnut

Take a real example , For example, you go to the bank to open the safe , among *p Operation, such as opening the safe , You have to take your cabinet number and find the safe in the bank first , Then take the key and open the safe to take out the money ; It's like p Stored in is an address , You take it first p Address stored in , Find that space in memory , Then again *p operation , Take out the value stored in that space .

therefore * Operation is value taking operation , That is, take out the data stored in the address where the pointer variable is stored .

 

6.2 * How to analyze the operation

The above examples are pointer value operations , That is, take the address number stored in the pointer variable and look for what is stored in the memory .

So look at how many of these are connected * An expression followed by a variable , It needs to be thrown away from right to left

Namely :***m ; namely * ( * ( * m ))), When looking, you need to throw it away from the innermost layer .

 

* *p ;
*
First as *(p), Look inside and out , First, he has a variable p, All can be directly obtained from memory first p Stored data 6618636(p Data in ), Then find the memory 6618636 That piece of memory , Finally take out 6618636 Stored data in 5( Specific data )
 

* **q = 5 ;
* First as *( * (q)), Look inside and out
* Get it from memory first q Stored data 6618624(q Data in ), Then find it in memory 6618624 That piece of memory , Take out the stored data 6618636(*q Data stored in ), It's done
* (q) operation , Then find it in memory 6618636 That piece of memory , Take out the stored data 5 ( *( *(q)) Data stored in ), It's done  *(*
(q)) operation , Stored data in re 5( Specific data ) ( How many? * Find several layers )
 

* ***m = 5
* First as *(*( * (m))), Look inside and out
* Get it from memory first m Stored data 6618616(m Data in ), Then find it in memory 6618616 That piece of memory , Take out the stored data 6618624(*q Data stored in ), It's done
* (m) operation , Then find it in memory 6618624 That piece of memory , Take out the stored data 6618636 ( *( *(m)) Data stored in ), It's done  *(*
(m)) operation , Then find it in memory 6618636 That piece of memory , Take out the stored data 5( *(*( *(m))) Data stored in ), It's done *(*( *(m))) operation ,
( How many? * Find several layers )
 

study c Language for years , This is my understanding of pointers , If you have different views , Welcome comments and discussion !!!

Technology