Dark Horse Programmer-----Pointer

Source: Internet
Author: User

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

First, the basic concept of pointers

First, the basic concept

The number of the memory unit is called the address. The desired memory unit can be found based on the number or address of the memory unit, so this address is often referred to as a pointer.

The contents of a memory unit's pointer and memory unit are two different concepts.

Summary: For a memory unit, the address of the cell is a pointer, where the data stored is the contents of the unit.

Ii. Benefits of using pointers

A. Provides a flexible means of modifying the calling variable for a function.

B. Let the function have multiple return values. Simple Calculator

1 #include <stdio.h>

Define a function, with plus, minus, multiply, except 2 void caculator (int x,int y, int *add, int *jian, int *cheng, float * Chu) {3//inside the function accesses the variable value in the Keynote function 4//addition pointer variable expression 5 *add = x + y; 6 7//Subtraction pointer variable expression 8 *jian = x- y; 9 10 Multiplication pointer variable expression *cheng = x * y;12 13//Division pointer variable expression 14 *chu = x/(float ) y;15 }17 int main (int argc, const char * argv[]) {18 19//define a pointer Variable, and initialize the *add = 0 , int *jian = 0 ; int *cheng = 0 ; float *chu = 0 ; 24 25//Call Calculato R This function is Caculator (12,4,&add,&jian,&cheng, & chu), printf ("add =%d\n" , add), and printf (" Jian =%d\n ", Jian), printf (" Cheng =%d\n ", Cheng); printf (" Chu =%d\n ", Chu); return 0 ; 32 }

C. The efficiency of certain subroutines can be improved.

>> when data is passed, if the chunk is large (such as a data buffer or a larger structure), you can use the pointer to pass the address instead of the actual data, which increases the transfer speed and saves a lot of content.

D. Provide support for dynamic data structures such as binary trees, linked lists.

third, the variable access mode

Access is divided into two types: direct access and indirect access.

Direct access: Assignment and value of variables (access value by variable name int a = 3; int b=a;)

Indirect access: Through the pointer (address) indirect operation is completed.

second, the concept of pointer variable and its definition method

First, the concept of pointer variables

The pointer is an address and is a constant.

A pointer variable is one that holds an address and is a variable. (Use a variable to hold the pointer or a variable holding the pointer (address); The address of the variable);

The value of a pointer variable is the address of a memory cell or a pointer to a memory unit.

Second, define a pointer variable

The definition of a pointer variable consists of three contents:

1) pointer type description, that is, define the variable as a pointer variable;

2) pointer variable name;

3) variable value (pointer);

The general form is:

Data type (type descriptor) * pointer variable name;

Iii. Considerations for the definition of pointer variables

1. "*" must have, if not equivalent to define an ordinary variable.

2. Can the pointer variable be used to hold values or characters? No.

3. A pointer of one type can only point to a variable of the same type and cannot point to another type of variable.

1       int num  = ten;  Define a constant num  , and initialize the value to 102       3       //define a pointer variable p, and point to Num's address 4       int  *p = &num;

4. Pointer variables, in the final analysis, are variables, as well as global and local variables.

Use of pointer variables: Define pointer variables-----> Initialize pointer variables-----> use (Indirect access to memory unit values using pointer variables)

What are the consequences of a pointer variable if it is not initialized?

Conclusion: If a pointer variable is defined later, if it is not initialized, the pointer variable holds a garbage number, which is the pointer variable, called the wild pointer.

Be sure to avoid manipulating the wild pointers, as they can lead to unexpected errors

int *P10; Value is a garbage value, it is possible to store the address of the System program

*P10 = 438; May cause system crashes

P10 = &a;

Iv. Initialization and reference of pointer variables

1     //How does the pointer variable initialize? The      pointer variable should be initialized with the address 2          3          //define two variables and give the variable an initial 4             int a =4,b =5; 5           6          //define the variable first and then initialize the variable value 7             int A1; 8             a1 =9      //1) The simultaneous initialization of the definition is           1. Full initialization of              int  *p  = &a;     Use the address of A to initialize p this pointer variable                                 //Another description method: P points  to a (A's address given p)           2. Partial initialization of               *p3 int = &b, *p4;   //Defines two pointer variables P3  p417               P4 =  &b;18 3. If a pointer variable is defined, but I don't know who to point this pointer to, int *p5 = NULL; /null is null (0) int *p6 = 0;//Empty 23 24//Assign a pointer to an empty, nothing is worth 25 26//Common easy-to-commit error int *P8 = 1000; Eliminate 28 29//2) First define post-initialize (preferably not so used) int *P7; p7 =         

* Pointer variable action: Gets the contents of the memory space that the pointer variable points to

int value = *P1; Gets the storage unit that corresponds to the value of the pointer variable

Assigning a value to the memory cell that the pointer variable points to

*P1 = 100; Set Value

value = *P1; 100

Two usages of "*"

1) used to define a pointer variable

Store the contents of the storage that the pointer variable points to

The difference between pointers and pointer variables:

1. The pointer is an address (the address is immutable) and is a constant.

2. The pointer variable is a variable that holds an address. (the variable that stores the address)

The third application: using functions to realize two-variable value exchange

Thinking & implementation:

A function is used to realize the exchange of two variable values.

1           

void Swap (int *p, int *p1) {2 3 //define a variable 4 int temp; 5 6 //Swap values for two variables 7 temp = *p; 8 *p = *p1; 9 *p1 = temp; Ten }12 int main (int argc, const char * argv[]) {13 14//define two variables in int a=4,b=5; printf ("before swap: a =%d, b =%d " swap (A, b); printf (" after exchange: A =%d,b=%d ", A, b); return 0

Iv. Common application scenarios for pointers

I. Common application scenarios for pointers

   

1                 int   a  =10; 2                 int  *p  = &a;3                 *p  =   4                 int  b   =  *p;  

1) The values of the variables in the keynote function can be modified in the modulated function

1/    * 2             defines a function 3             to modify the contents of a pointer variable by a pointer variable inside a function 4             p     pointer variable 5    */6         #include <stdio.h>     7         void changvalue (int *p) {8  9                *p  =       }11         int  main (int ARGC,  const  char * argv[]) {  a 1 int a = a;               int *p1  =  &a;   P1 points to A14               ("%p\n", &a);  Address:               printf ("%p\n" Changvalue (p1), printf ("%d\n", *P1),//10021 printf ("%d\n", a); return 0; 23} 

2) allow the function to have multiple return values

There are not multiple return

Simple Calculator

fifth two-level hands

Secondary pointer Introduction

If a pointer variable holds the address of another pointer variable, the pointer variable is called a pointer variable pointing to the pointer. Also known as the "level two pointer"


1       #include <stdio.h> 2        3        int main (int argc,  const  Char  * argv[]) {4           5        int A = 5; 6         7        //define a pointer variable 8        int *p  = &A; 9        printf ("p =%p\n", p);        printf ("& Amp;a =&p\n ",&a);        One/        /define a level two pointer/        /* * Number equal to series        int  **p1 = &p;15 printf ("&p =%p\n", &< C25>P); printf ("P1 =%p\n", p1); printf ("*p =%d\n", *p); The value of the//p, the address of a ("*P1 =%p\n", *p1 ); printf ("**p1 =%d\n", **P1); 522 return 0;         

Why do pointers differentiate between types

Conclusion: Define what type of pointer should point to what type of variable. You can't just point it out, or you don't mean more or less. It's not right to read and read more.

Dark Horse Programmer-----Pointer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.