Your Ad Here

Tuesday, September 9, 2008

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 data types that may be referenced under a single name. (See
Chapter 7 for a discussion of structures and unions.)
The dot operator is used when working with a structure or union directly. The arrow operator is used
with a pointer to a structure or union. 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: