Structure and union members are accessed using the following two selection operators:
The operator . is called the direct member selector and the operator -> is called the indirect (or pointer) member selector. For example:
struct mystruct { int i; char str[21]; double d; } s, *sptr = &s; . . . s.i = 3; // assign to the i member of mystruct s sptr -> d = 1.23; // assign to the d member of mystruct s
The expression s.m is an lvalue, provided that s is an lvalue and m is not an array type. The expression sptr->m is an lvalue unless m is an array type.
If structure B contains a field whose type is structure A, the members of A can be accessed by two applications of the member selectors
struct A { int j; double x; }; struct B { int i; struct A a; double d; } s, *sptr; . . . s.i = 3; // assign to the i member of B s.a.j = 2; // assign to the j member of A sptr->d = 1.23; // assign to the d member of B sptr->a.x = 3.14 // assign to x member of A
Each structure declaration introduces a unique structure type, so that in
struct A { int i,j; double d; } a, a1; struct B { int i,j; double d; } b;
the objects a and a1 are both of type struct A, but the objects a and b are of different structure types. Structures can be assigned only if the source and destination have the same type:
a = a1; // OK: same type, so member by member assignment a = b; // ILLEGAL: different types a.i = b.i; a.j = b.j; a.d = b.d /* but you can assign member-by-member */
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|