Tuesday, September 21, 2010

The Dot (.) and Arrow (−>) Operators

In C, the . (dot) and the −>(arrow) operators access individual elements of structures
and unions. Structures and unions are compound (also called aggregate) data types that
may be referenced under a single name (see Chapter 7). In C++, the dot and arrow
operators are also used to access the members of a class.
The dot operator is used when working with a structure or union directly. The
arrow operator is used when a pointer to a structure or union is used. For example,
given the fragment
struct employee
{
char name[80];
int age;
float wage;
} emp;
struct employee *p = &emp; /* address of emp into p */
you would write the following code to assign the value 123.23 to the wage member of
structure variable emp:
emp.wage = 123.23;
However, the same assignment using a pointer to emp would be
p->wage = 123.23;

No comments:

Post a Comment