Pages

Friday, December 28, 2012

Accessing Members of a Structure - C Programming


For accessing any member of a structure variable,we use the dot (.) operator which is also known as the period or membership operator.The format for accessing a structure member is -

  structvariable.member

Here on the left side of the dot there should be a variable of structure variable of structure type and right side there should be the name of a member of that structure.

For example ::
struct student {
char name[20];
int rollno;
float marks;
};
struct student stu1;

name of stu1 is given by -stu1.name same as stu1.rollno and stu1.marks.
++stu1.marks;
The dot operator is one of the highest precedence operators,its associativity  is from left to right. Hence it will take precedence over all othe unary,relational,logical,arithmetic and assignment operators. So above expression ,first stu1.marks will be accessed and then its value will be increased by 1.

No comments:

Post a Comment