mikroC

making it simple...

Structure Member Access

mikroC - C Compiler for Microchip PIC microcontrollers

Structure and union members are accessed using the following two selection oper- ators:

. (period)

->(right arrow)

The operator . is called the direct member selector and it is used to directly access one of the structure’s members. Suppose that the object s is of struct type S. Then if m is a member identifier of type M declared in s, the expression

s.m // direct access to member m

is of type M, and represents the member object m in s.

The operator ->is called the indirect (or pointer) member selector. Suppose that ps is a pointer to s. Then if m is a member identifier of type M declared in s, the expression

ps->m // indirect access to member m;

// identical to (*ps).m

is of type M, and represents the member object m in s. The expression ps->mis a convenient shorthand for (*ps).m.

For example:

struct mystruct {

int i; char str[10]; 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->mis an lvalue unless m is an array type.

 

 

page

MikroElektronika: Development tools - Books - Compilers

77