Structures, pointers, and members

Source: Internet
Author: User
Tags value of pi

accessing structures and their members directly through pointers is very simple, but they are easily confused when applied to complex situations. Here's an example:

typedef struct

{

int A;

short b[2];

}Ex2;

typedef struct Ex

{

int A;

Char b[3];

Ex2 C;

Ex *d;

}Ex;

The structure of the type ex can be represented by the following diagram:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7D/F9/wKiom1bz29yy0-JQAAAee0mxCsY171.png "title=" Qq20160324161610.png "alt=" Wkiom1bz29yy0-jqaaaee0mxcsy171.png "/>

We use a graph to represent the structure above, which makes the above example look clearer. In fact, this picture is not entirely correct, as the compiler will try to avoid space wasting between members whenever possible.

The above example uses this statement:

Ex x = {ten, "Hi" , {5, {-1, +}}, 0};

Ex *px = &x;

They will produce the following variables

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/F5/wKioL1bz3MeTs_EMAAAkYcpVkaA734.png "title=" Qq20160324162703.png "alt=" Wkiol1bz3mets_emaaakycpvkaa734.png "/>

Access pointers

Let's start with the pointer variable, the right value of the expression px is:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/F9/wKiom1bz3GDiaJKiAAAkLtApQFQ723.png "title=" Qq20160324163622.png "alt=" wkiom1bz3gdiajkiaaakltapqfq723.png "/> px is a pointer variable, but there is no indirect access operator here, so the value of this expression is the content of PX. The left value of this expression:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/F5/wKioL1bz3R7yFrnMAAAjkl_b5PA728.png "title=" Qq20160324171934.png "alt=" Wkiol1bz3r7yfrnmaaajkl_b5pa728.png "/>

It shows that the old value of PX will be replaced by an lvalue.

Now consider px+1. This expression is not a valid lvalue because its value is not stored in any identifiable memory location. The right-hand value of this expression is more interesting. If the PX points to an element of an array of structures, the expression will point to the next structure of the array. But even so, this expression is still illegal, because we can't tell if the next location of memory is storing one of these elements or something else. The compiler cannot detect such errors, so you must decide for yourself whether the pointer operation makes sense.

Access structure

We can use the * operator to perform indirect access to the pointer, and the right value of the expression *px is the entire structure that the PX points to.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7D/F5/wKioL1bz3UTzCE12AAAk55i1lUQ986.png "title=" Qq20160324182633.png "alt=" wkiol1bz3utzce12aaak55i1luq986.png "/> Indirect access operation with the arrow access structure, you can assign this expression to another type of the same structure, You can also use it as the left-hand operand of the dot operator to access a specified member. You can also assign it as a parameter to the function, or return it as the return value of the function (however, there is a need to consider the efficiency issue with regard to the last two operations). The left value of the expression *px is:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/F9/wKiom1bz3NGhxr-5AAAicLgiYMU622.png "title=" Qq20160324183547.png "alt=" Wkiom1bz3nghxr-5aaaiclgiymu622.png "/>

Here, the struct will accept a new value, or, more precisely, it will accept the new value of all its members. As an lvalue, it is important to position, not the value that is saved in this position.

The expression *px+1 is illegal because the result of *PX is a structure. There is no addition in the C language that defines the structure and the value of an integral type.

But what about * (px+1)? If x is an array element, the expression represents the structure behind it, but X is a scalar, so the expression is actually illegal.

accessing struct members

The right-hand value of the expression Px->a is:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7D/F5/wKioL1bz3ZHSKrQDAAAk_FvJztg851.png "title=" Qq20160324184904.png "alt=" Wkiol1bz3zhskrqdaaak_fvjztg851.png "/>

The operator performs an indirect access operation on PX, which first obtains the structure it points to the data and then accesses member A. When you have a pointer to the structure but do not know the name of the structure, you can use the expression Px->a, if you know the structure of the name, you can also use X.A.

Here we compare *px and px->a to each other. In both expressions, the address that the PX holds is used to find its structure. But the first member of the struct is a, so the address of a is the same as the address of the structure, so that PX appears to point to the entire structure and to the first member of the struct, after all, they have the same address. However, only half of this analysis is correct, although the values of two addresses are the same, but they are of different types. The PX is declared to be a pointer to a struct, so *px is pointing to the entire structure, not his first member.

For example, we create a pointer to shaping:

int *pi;

Can we have pi point to integer member A? If the value of pi is the same as PX, then the result of the expression *pi will be member A.

However, an expression

PI = px;

are illegal because they are of different types and can be effective with coercion type conversions:

pi = (int *) px;

However, this approach is dangerous because it avoids the compiler's type checking. The correct expression is simpler ———— use the & operator to get a pointer to Px->a:

PI = &pi->a;

Let's take a look at the diagram of &pi->a:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7D/F9/wKiom1bz3RehsdeCAAAnBFjtrSk697.png "title=" Qq20160324191446.png "alt=" Wkiom1bz3rehsdecaaanbfjtrsk697.png "/>

Notice how the values in the ellipse directly access member A, which is the opposite of PX, which points to the entire structure. The PI and PX have the same value after the assignment operation above. But their types are different, so the value of indirect access to them is also different, *px is the entire structure, and *PI is a single integer value.

Let's look at an example below. The value of px->b is a pointer constant, because B is an array, and the expression is not a valid lvalue. Below it's right value is:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/F6/wKioL1bz3cugWZUjAAAnHIy8CJ8304.png "title=" Qq20160324194459.png "alt=" Wkiol1bz3cugwzujaaanhiy8cj8304.png "/>

If an indirect access operation is performed on the expression, it accesses the first element of the array, uses the subscript reference or pointer operation, and we can also access the other elements of the array, and the expression Px->b[1] accesses the second element of the array.

Access to nested structures

In order to access itself is also a struct member C, we can use Px->c, whose left value is the entire structure.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7D/F9/wKiom1bz3VOTSHFbAAAkm-UrLNc093.png "title=" Qq20160324195401.png "alt=" wkiom1bz3votshfbaaakm-urlnc093.png "/> This expression can access a specific member of C with a point-manipulation method. GCA, PX->C.A has the following right values:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/F9/wKiom1bz3XDiS8cUAAAkNz0wzio182.png "title=" Qq20160324195757.png "alt=" Wkiom1bz3xdis8cuaaaknz0wzio182.png "/>

Accessing pointer members

The expression px->d its right value is 0, and its left value is the memory location of its own. The expression *px->d this is wrong. Here the indirect access operator acts on the pointer value stored by member D, but D contains a null pointer, so it does not point to anything, and a null pointer dereference operation is wrong.

The right value of the expression Px->d->c.b[1] is as follows:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7D/F6/wKioL1bz3ivhLnCEAAA5jaBmna0646.png "title=" Qq20160324201450.png "alt=" Wkiol1bz3ivhlnceaaa5jabmna0646.png "/>

Structures, pointers, and members

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.