Usage of pointers in C language [1]

Source: Internet
Author: User

 

Pointer is an important concept in C language and also an important feature of C language. Using pointers can make the program more concise.

1. storage of data in the memory: allocate a certain number of storage units (bytes) to the Data Type in the memory ).

2. Address of the memory unit: the number of the memory unit, which corresponds to the variable name.

3. memory unit content: Value

4. Direct Access to variables: the way to access variable values by the variable address (that is, the variable name.

5. indirect access to variables: place the variable address in another memory unit, first obtain the variable address in another memory unit, and then find the variable address for data access.

The variable pointer is a pointer variable that stores the memory address of variable I. We can say that pointer Points to the address of I.

6. pointer concept: the address of a variable is called the pointer of the variable.

7. pointer variables: variables used to store other variable addresses.

The difference between a pointer and a pointer variable, that is, the difference between a value and a variable.

Pointer variable definition:

1. Definition method: type identifier * pointer variable name;

2. Three elements of pointer variables:

Pointer variable type: consistent with the type of the Variable

Pointer variable value: address of another variable in memory

Pointer name: Starting from normal variables

3. Description

* Before the identifier indicates that the variable is a pointer variable, but * is the name of the pointer variable.

 

Pointer variable assignment: 1. pointer variable name = an address; (& get an address character)

2. pointer variable name = another assigned pointer variable

Example: int I, * p, * q;

P = & I; // method 1

Q = p; // method 2

Pointer operation

 

Example: int * p, I;

Char * q, c;

P = & I; q = & c;

P ++, q ++;

 

P ++, q ++; // after execution is complete

 

Because the int type pointed to by p occupies two bytes of space (generally) and points to 1002 after auto-increment, while the char type occupies one byte, q points to 2001

 

 

Comparison: p> q // false (use the above Program)

 

Int a [10];

Int * p, * q;

P = & a [0]; // or p =;

Q = & a [9]; // or p = a + 9;

P <q, p> q is true or false?

The figure shows that p <q is true.

 

 

Summary: Two operators:

*: Find the content in the memory address.

&: Find the memory address of the Variable

Summary of the following small program

Main ()

{

Int a, B;

Int * pointer_1, * pointer_2;

A = 100;

B = 10;

Pointer_1 = &;

Pointer_2 = & B;

Printf ("% d, % d \ n", a, B );

Printf ("% d, % d", * pointer_1, * pointer_2 );

 

}

The running result is, 10

100,10

Graphically: (p1 and p2 represent pointer_1 and pointer_2 respectively)

The following are several changes:

 

1. After executing the statement p2 = & * pointer_1, it becomes the form. In this case, * pointer_1 is a, * p2 is a, p1 = p2 = &

2. What is the meaning of "& * pointer_1?

 

3. What is the equivalent of (* pointer_1) ++?

4. * What does & a mean?

Related Article

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.