Introduction to C language understanding pointer

Time:2021-4-21

This article is my original, suitable for just entered the pit C language, for the definition and usage of pointer ambiguous students, if there is any error, please point out.

 

Basically, pointer variables are also variables, justintIt becameint *And so on. It’s just that the content of the pointer variable is the address of the ordinary variable in the storage space (some strange thing)16Base address format, interested in your own (Baidu)

Define the format of the pointer variable:int/double/…* studentp;

Therefore, when assigning values to pointer variables, you should:studentp=&student;

So how to get it through pointerstudentCan be directly: a variable=*studentp; Don’t confuse it with defining pointer variables here!!!

 

A function pointer is the same as an ordinary pointer, except that the function name is given to a function pointer variable. When getting the function that the pointer variable points to, a pointer variable is added in front of the pointer variable*number

 

Well, someone asked me, the code I wrote has never used pointer variable before, and it still works. Is pointer variable superfluous?

It’s a classic questionswapExchange – if you want to define aswapFunction, exchange the value of two variables, how to achieve?

Some people say that the following code on the line, and did not use the pointer ah!

#include 
 
 void swap(int a,int b);
 int main(void) { 
     Printf ("little lemon" class begins! \n");
     
     int a=5,b=6;
     swap(a,b);
     printf (" Main function interior a =% d , b =% d \ n ", a , b );
     return 0;
 }
 void swap(int a,int b){
     int tmp;
     tmp=a;
     a=b;
     b=tmp;
     Printf ("swap function internal a = - D, B = - D, n", a, b));
 }

Output:

 

A:CLanguage programming is process oriented programming. Process oriented programming is to analyze the steps needed to solve problems, and then use functions to realize these steps step by step. When using, it can be called one by one. If I need to continue to use it in the main functionabExchange the completed value, then the function will fail to write, andCThere can only be one language return value, so the returned operation cannot be used to get the result.

One might ask, why do IswapIt’s clear that it’s not easyabExchange, the results inmainThere’s no use in functions?

A: this involves the principle of operation mechanism. Each function monopolizes a memory space,swapSpatialabandmainSpatialabObviously not the same pairab,So no matterswapHow to toss can not affectmainWhat’s inside.

 

If pointer variables are used, the result will be different.

Now change the code to a pointer variable:

#include 
 
 void swap(int *a,int *b);
 int main(void) { 
     Printf ("little lemon" class begins! \n");
     
     int a=5,b=6;
     swap(&a,&b);
     Printf ("in the main function, a = D, B = D, n", a, b));
     return 0;
 }
 
 void swap(int *a,int *b){
     int tmp;
     tmp=*a;
     *a=*b;
     *b=tmp;
     printf (" Swap function internal a =% d , b =% d \ n ",* a ,* b );
 }

Output:

 

hereswapThe two formal parameters of are pointer variables used to receivemainFunctiona(main)andb(main)Your address,swapMiddle pointer variablea(swap)andb(swap)havea(main)andb(main)The address of the*aand*bChange the way you want tomainina(main)andb(main)The value of.

This is the advantage of using the pointer.

Recommended Today

Backup and file back mechanism of tcapsusdb

Backup and file back mechanism of tcapsusdb With the rapid development of e-commerce and office online, enterprises are more and more dependent on information system. Database, as the core of information system, plays an important role. For the database, due to the huge amount of data and very important, every data loss may be a […]