Pointer step analysis in C Language

Source: Internet
Author: User

Basic explanation

Through the analysis in the previous article, we know clearly that a pointer is not a simple type, and it is a combination of itself and the pointed object. The arithmetic operations (such as stepping) of pointers are closely related to the types of objects pointed to by pointers.

Problem: pointer step & amp; Step Unit

The followingCodeWhat are the output results?

Int arcontext [5] = {0, 1, 2, 3, 4}, I, * par;
Par = arcontext;
Printf ("% d \ n", * (PAR + 3 * sizeof (INT )));

Answer and analysis:

This Code does not have a correct answer. Because this code is wrong, printf will output an unpredictable value in the memory zone, for the following reasons:

In C, pointers always follow the size of the object it points. In the preceding example, par is a pointer to an integer variable. An integer is 4 bytes (the default CPU length is 32 characters), and par + 1 points to the next integer, that is to say, the pointer moves four bytes behind, instead of moving the address only one byte.

Because the C language compiler knows the type of each pointer, the pointer operation automatically considers the size of the indicated type.

Par + 3 * sizeof (INT) = par + 3*4 = par + 12, so the par points to the 13th integer element of the array. The array itself has only five elements. The PAR has already exceeded the limit and points to something that is unknown to no one. The specific points are different compilers. In short, we certainly cannot print out the value we want.

A pointer is not a simple type. It is a type that is composite with the type of the pointer. Therefore, its arithmetic operations are closely related to the type of the object referred to by the pointer, and are the same in C ++.

The following is an example:

Int A [8];
Int * P =;
Int * q = P + 3;
P ++;

Addition and subtraction of a pointer is not a binary representation of the pointer itself. Remember that a pointer is the address of an element. Every time it is added, it points to the next element. Therefore:

Int * q = P + 3;

Q points to the third integer starting with P.

P ++;

P points to the next integer.
Problem: pointer step-by-step unit conversion

I have a char * type pointer that points to an int type value. I want this pointer to skip int to the next Char. Can the following code achieve this purpose?

(Int *) P) ++;

Answer and analysis:

Yes.

First, we need to understand the concept of the left and right values in C. In C, the left value can be placed on the left of "=", that is, it can be assigned a value, the right value can be placed on the right of "=", that is, the value that can be assigned to other variables. ++ Is a single object operator. It adds the value of a variable to 1 and then assigns it to the variable. Therefore, the required operands can be placed on the left of the "=" sign, it can also be placed on the right of "=. In principle, the result of type forced conversion is the right value rather than the left value. Therefore, the result of (int *) P is the right value of ++ in this expression, while the left value of ++ is still P rather than (int *) p.

The core of this problem is to tell us that the result of type forced conversion is the right value rather than the left value.

In addition, we can use a simple method to achieve the same purpose:

P + = sizeof (INT );

P is a char * pointer. Its step length is 1, and the length occupied by an integer is skipped.

So, sometimes, ulong * P; to add 8 bytes, you can perform the following forced conversion:

(Ulong *) (uchar *) P + 8

Problem: pointer step & void pointer

Why does the compiler report the following error when I perform operations on Void * pointers?

Error c2036: 'void * ': Unknown size

Answer and analysis:

In C, all pointers, such as +,-, *, And/, are far-fetched. For example, if the 'Char * 'type pointer is added to 1, the address moves one byte backward. If the 'int *' type pointer is added to 1, it moves four bytes. But what about the 'void * 'pointer? The 'void * 'pointer is specified in the C standard to be forcibly converted to any type of pointer without data loss. The specific size of the pointer varies with the compiler, that is, the compiler does not know how big the void is. Therefore, it is impossible to perform arithmetic operations on pointers of the 'void* 'type.

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.