Listen Weng Kai speak C language pointer 8-

Pointer ( key of c )

Memory is an operating system abstraction of physical memory - virtual memory.

The virtual memory can be viewed as a continuous small squares, each square is the size of one byte (byte) = 8 bit, can store a binary number of eight, each of the small box 1 will have a number , the virtual memory size is 2 2gb range 1024 ^ 3 byte number from 0 to 1024 ^ 3-1 image-20200309143609430pointer about analysis: pointer is a variable, the value may become his, his address which is stored Suppose we define a pointer to int p; int a; if p = & a; this time we know, a is a variable, then the memory must give him assign an address to store him, p points to a meaning that is p kept inside is a address . So p is a deposit with variable addresses. Now that we know which kept a p address, so we are looking for a very convenient, * p value of p it means the address stored inside, which is the value of a. This sentence is how to understand it?

~ .p address is stored, compared to where you live, then p to represent you, because p would indicate that the address data inside. This time p and a is exactly the same, and if you want to change a value. a = a + 1 and P = P +. 1 is exactly the same, to achieve the purpose of operating a. However, p = p + 1 and p = (p + 1) is not the same, this time with special attention because p which is the address, then (p + 1) says that after this address plus 1, address plus 1 is not that a change of address? After a change of address on the inside is certainly not a deposit, just may be your neighbors. Because the address has changed, so p is changed, and therefore * has changed, this place is a little difficult to understand, the landlord more than pondering pondering. To sum it up: A pointer is a master key, can point to any address, you can change any address inside the data (except for read-only), using pointer pay attention to safety, in order to avoid an exception occurs.

1, the operator &

scanf ( "% d", & i); // here is an operator &

· Role: to obtain the address of a variable, its operand must be a variable.

· In the C language, sizeof () is a data type determines the length of the operator or expression

​ ·int i;printf("%x",&i);

`int i; printf ("% p ", & i); // address output by the address and size of the int% p depends extended compilers:% X means is a hexadecimal number in the form of integer output

1.% c: A character is read

2.% d: decimal integer

3.% f,% F,% e,% E,% g,% G for inputting a real number, or can be entered as a decimal exponent

4.% o: octal

5.% s: read into a string, the case of spaces, tabs, or new end.

6.% u: unsigned decimal number

7 %%: percent sign% output 

8.% i read in decimal, octal, hexadecimal integer

9.% p a read pointer 

10.% n characters equivalent to this value has been read

11% [] set of characters scanned  

12.% a,% A reads a floating point value (valid only C99) 

& Can not be advanced after taking out the address of something without an address, top-down stack

a, a pointer is a variable holding address

​ int i;

* p = i & int; : p represents a pointer to int, to the address of i p p represents the value of i. p i at the address where the value of that variable, then said p points to i.

* P int, Q; int P, Q; // P is an int, q is not a pointer. Whether and int and p are close or relying on the past. int P, Q *; case p, q is a pointer to the.

p is an int numbers, p * p is a pointer.

· P is the name of a pointer variable indicates the memory address of the pointer variable points to, if you use% p to output, it will be a hexadecimal number

· * P represents the content of this memory address pointer stored in, and is generally consistent with a pointer type variables or constants

· Is & address operator, is to take the address pointer p & p,

b, value is the memory address of the variable.

· Common variable is the value of the actual value.

· Pointer variable value is the address of a variable with real values.

c、

void f (int * p); f need int pointer // get the address of a variable is called at the time.

int i = 0; f (& i); if this function is called when it should be left to an address with the address & made variable, this variable is then passed to the pointer. // this i can access the outside through this pointer in a function.

#include <stdio.h>
void f(int *p);               
void g(int k);
int main(void)         
{                 
   int i = 6;          
   printf("&i=%p\n", &i);         //&i:地址值&i被传进了函数f中              
   f(&i);               
   g(&i);               
   return 0;          
}              
void f(int *p)              //p:通过这个地址             
{                
   printf(" p=%p\n", p);     
   printf("*p=%d\n", *p);       
   *p = 26;                 //*p:用*p这种方式访问到i变量             
}                //指针就是保存了内存的一个地址(门牌号),通过这个地址就能够访问对于内存中的数据 
void g(int k)               
{              
   printf("k=%d\n", k)                   //***p就是i=6的值,而p=&i,所以p和&i都是地址。 \*地址=值 注:\*p可以是和指针类型相同的变量也可以是常量**     
}                        //p是i的地址,*p就是i(的值)。

image-20200309143829064

d、

* Is a unary operator, - when the definition is merely illustrative of pointer variables

· Access to this variable pointer stored address

· Pointer address used to access the value of the variable on the address indicated.

· Can be left value may also be the right value.

​ ·int k=*p;

​ *p=k+1;

f, value left

An assignment on the left is not a variable, but the value is the result of the calculation expression.

a [0] = 2; * p = 3; these two variables are not left.

g, reaction

& yptr is -> (& yper) -> (yper address) -> both yper reaction is to represent the original that variable & yper -> & (* yper) -> & (y) -> get y address -> yper

h, incoming address

· I = 6; int i; scanf ( "% d", i); this way is input to the compiler not being given, then the operation will be an error in the 32-bit integer as large as the address and the compiler mistaken i address is passed, the actual upload into the value of i is 6.

Practical application i, the pointer

I, the exchange value of two variables
#include <stdio.h>
void swap(int *pa, int *pb);
int main()         
{        
   int a = 5;      
   int b = 6;      
   swap(&a,&b);            //将a b的地址传递过awap函数中       
   printf("a=%d b=%d\n", a, b);            
   return 0;          
}
void swap(int *pa, int *pb)       //参数是两个指针,如果不用指针,那么从main函数传来的就是值,而不是变量。          
{           
   int t=*pa;             //*pa取出pa所代表的变量,赋值给t     
   *pa=*pb;           
   *pb= t;            
}

B, two application function that returns a pointer operation state, the result returned by the pointer

Guess you like

Origin www.cnblogs.com/wpoem/p/12448655.html