C language-pointer operation

Source: Internet
Author: User

I. "&" and "*":

The "&" operation in a single object is the address of the operation object, and "*" is the content of the object pointed to by the pointer. The two are inverse operations for each other.

 
IntX,*P;
P= &X;

& (* P) = P indicates the pointer; * (& X) = X indicates the variable X.

II: Arithmetic operation of pointer:

The pointer operation is related to the base type. Generally, if p is a pointer and N is a positive integer, the actual address after the + (-) operation on the pointer P is:

P+(-) N*Sizeof(Base type)
 Char    *  P;
Int * Q;
Float * TK;
// Assume that the current address is: P = 2110 H, q = 2231 H, TK = 2478 H
P + = 1 ; // P = p + 1 = 2111 H
Q + = 4 ; // Q = q + 4*4 = 2239 H
TK -= 3 ; // TK = TK-3*8 = 246ch

3. pointer auto-increment and auto-subtraction:

Pointer auto-addition and auto-subtraction are also address operations.

M= *P++Equivalent to m= *(P++)

The pointer P is used to assign the value of the variable currently referred to the variable M. P is used to add 1 to point to the next target variable.

M= * ++P is equivalent to M.= *(++P)

P adds 1 to point to the next target variable. Take the pointer P and assign the value of the variable currently referred to the variable M.

M=(*P)++And m= ++(*P)

The former is to assign the value of the variable referred to by the pointer P to the variable M, and then the variable * P automatically adds 1;

The latter assigns the value of * P, the variable referred to by P, to the variable M after adding 1.

4. subtraction between pointers:

Subtraction can be performed between pointers pointing to the same group of data types. The result of subtraction indicates the number of data records that are separated by two pointers.

V. relational operation of pointers:

P and q point to the same array. P> q indicates whether P points to the end of the position indicated by Q. If yes, the expression value isNon-0Otherwise, the value is 0.

6. subscript operation of the pointer:

P [I] = * (p + I );

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.