Getting Started with C language comprehension pointer

This article is my original, suitable for newly recruited pit C language for the definition and usage pointer blurred students, if not correct, please point out.

 

Fundamentally, the pointer variable is variable, but int becomes int * , and so on. But the pointer variable content is placed inside the ordinary variables in memory address space (a weird 16 hexadecimal address format, interested in self-Baidu)

Pointer variable format definition: int / Double /...* studentp;

So it should be in time to give a pointer variable assignment: studentp = & Student;

So how to get through a pointer student values can be straightforward: a variable = * studentp; there must not be confused with pointer variables and definitions! ! !

 

Ordinary function pointers and pointers as fact, nothing more than the name of the function to a function pointer variable, the function pointer variable points to get the time it was before the variable pointer plus a * No.

 

Then someone asked, I wrote the code did not previously used a pointer variable still running, the pointer variable that is superfluous thing?

It is not inevitable, and a classic question: swap exchange - If you want to define a swap function, swap the values of two variables, how to achieve?

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

. 1 #include <stdio.h>
 2  
. 3  void the swap ( int A, int B);
 . 4  int main ( void ) { 
 . 5      the printf ( " little lemon commence the \ n-! " );
 . 6      
. 7      int A = . 5 , B = . 6 ;
 . 8      the swap (A, B);
 . 9      the printf ( " master internal function D% = A, D B =% \ n- " , A, B);
 10      return  0 ;
 . 11  }
 12 is  void the swap ( int A, int b){
13     int tmp;
14     tmp=a;
15     a=b;
16     b=tmp;
17     printf("swap函数内部a=%d,b=%d\n",a,b);
18 }

Output:

 

Answer: C language programming is a process-oriented programming, process-oriented analysis is a required step to solve the problem, then use the function to implement these steps, step by step, when used in a call to a turn on it. If I need to continue to use the main function inside ab exchange value completed, the function to write very failure, and C language return only one value, you can not use the return operation to get the result.

One might ask, why do I swap which clearly let ab exchange, resulting in the main did not use function inside it?

A: This involves the principle of the operational mechanism, each function are exclusive memory space, swap space ab and the main space ab clearly not the same for ab, so no matter swap how frustrating can not affect the main things inside.

 

If you use the pointer variable, then the result will be different.

Now change to the code pointer variables:

. 1 #include <stdio.h>
 2  
. 3  void the swap ( int * A, int * B);
 . 4  int main ( void ) { 
 . 5      the printf ( " little lemon commence the \ n-! " );
 . 6      
. 7      int A = . 5 , B = . 6 ;
 . 8      the swap (& A, & B);
 . 9      the printf ( " master internal function A =% D, B =% D \ n- " , A, B);
 10      return  0 ;
 . 11  }
 12 is  
13 is  void the swap ( int *a,int *b){
14     int tmp;
15     tmp=*a;
16     *a=*b;
17     *b=tmp;
18     printf("swap函数内部a=%d,b=%d\n",*a,*b);
19 }

Output:

 

Here swap the two parameters is a pointer variable, for receiving the main function coming A (main) and B (main) address, swap pointer variable A (swap) and B (swap) deposit with A (main) and B (main) address, so that by * a and * b changes the direct mode main medium a (main) and B (main) value.

This is the advantage of using the pointer.

Guess you like

Origin www.cnblogs.com/Kristal/p/12430383.html