Let you no longer fear pointer 2-arithmetic operations on pointers

Source: Internet
Author: User

 

The pointer can be added or subtracted from an integer. The meaning of this operation of pointer is different from that of addition and subtraction of common values, in units. For example:

 

Example 2:

 

Char a [20];

 

Int * ptr = (int *) a; // The forced type conversion does not change the type of.

 

Ptr ++;

 

In the preceding example, the pointer ptr is of the int type * and it points to the int type. It is initialized to the integer variable. In the next 3rd sentences, the pointer ptr is added with 1, and the compiler processes it like this: it adds the value of the pointer ptr with sizeof (int), in a 32-bit program, 4 is added because int occupies 4 bytes in 32-bit programs. Because the address is in bytes, the address pointed by ptr is increased by four bytes from the address of the original variable a to the high address. Because the length of the char type is one byte, the ptr originally points to the four bytes starting from Unit 0th of array, this point points to the four bytes starting from Unit 4th in array.

 

We can use a pointer and a loop to traverse an array. For example:

 

Example 3:

 

Int array [20] = {0 };

Int * ptr = array;

For (I = 0; I <20; I ++)

{

(* Ptr) ++;

Ptr ++;

}

In this example, the value of each unit in the integer array is added to 1. Since every loop adds a pointer ptr to one unit, each loop can access the next unit of the array.

 

Let's look at the example:

 

Example 4:

 

Char a [20] = "You_are_a_girl ";

Int * ptr = (int *);

Ptr + = 5;

In this example, ptr is added with 5, and the compiler processes it like this: add the value of the pointer ptr to the value of the 5-bysizeof (int ), in the 32-bit program, 5 is multiplied by 4 = 20. Because the address unit is byte, the current ptr points to the address, compared to the address pointed by the ptr after the addition of 5, move 20 bytes to the high address. In this example, if the ptr before 5 is not added, it points to the four bytes starting with unit 0th of array a. After 5 is added, the ptr points out of the valid range of array. Although this situation may cause problems in applications, it is possible in terms of syntax. This also reflects the flexibility of pointers.

 

If in the above example, ptr is subtracted from 5, the processing process is similar, except that the ptr value is subtracted from 5 by sizeof (int ), the new ptr points to an address that moves 20 bytes to the lower address direction than the original ptr.

 

Here is another example: (A misunderstanding)

 

Example 5:

 

# Include <stdio. h>

Int main ()

{

Char a [20] = "You_are_a_girl ";

Char * p =;

<Span style = "color: # FF0000;"> char ** ptr = & p; </span>

// Printf ("p = % d \ n", p );

// Printf ("ptr = % d \ n", ptr );

// Printf ("* ptr = % d \ n", * ptr );

Printf ("** ptr = % c \ n", ** ptr );

Ptr ++;

// Printf ("ptr = % d \ n", ptr );

// Printf ("* ptr = % d \ n", * ptr );

Printf ("** ptr = % c \ n", ** ptr );

}

Misunderstanding 1. Output answer: Y and o

 

Misunderstanding: ptr is a second-level pointer of char. When executing ptr ++;, it will add a sizeof (char) to the pointer, so the above result is output, this may be the result of a small number of people.

 

Misunderstanding 2. Output answer: Y and

 

Misunderstanding: ptr points to a char * type. When executing ptr ++;, it will add a sizeof (char *) to the pointer (someone may think this value is 1, then you will get the answer to misunderstanding 1. This value should be 4, refer to the previous content), that is, & p + 4; isn't a single value operation pointing to the fifth element in the array? Isn't the output result The Fifth Element in the array? The answer is no.

 

Positive solution: the ptr type is char ** and the pointer type is char *. The pointer address is p (& p). When ptr ++ is executed, it will add a sizeof (char *) to the pointer, that is, & p + 4; Where does * (& p + 4) point to? Ask God, or he will tell you where it is? Therefore, the final output is a random value, which may be an invalid operation.

 

Summary:

 

After a pointer ptrold is added (subtracted) by an integer n, the result is a new pointer ptrnew. The type of ptrnew is the same as that of ptrold, ptrnew points to the same type as ptrold. The value of ptrnew will increase (decrease) n times of sizeof (type pointed to by ptrold) bytes than the value of ptrold. That is to say, ptrnew will point to a memory area that moves n x sizeof (the type indicated by ptrold) bytes to the high (low) Address direction than the memory area pointed to by ptrold.

 

Add or subtract pointer and pointer:

 

The two pointers cannot be used for addition operations. This operation is invalid because after addition, the result is directed to an unknown place and meaningless. The two pointers can be used for subtraction, but they must be of the same type. They are generally used in arrays.

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.