Accessing structure members
There are two different ways to access structure members
1) using dot (.) operator
2) using pointer to structure Accessing
structure members using dot operator In this dot(.) operator is used between structure variable and structure member name.
Example: struct StudentInfo s1;
s1.Rno;
Here โs1โ is variable and โRnoโ is member of structure StudentInfo. Accessing structure members using pointer
In this -> operator is used between structure pointer variable and member name.
Example: struct StudentInfo *ptr, s1;
ptr=&s1;
ptr->Rno;
Here โ*ptrโ is a pointer to structure StudentInfo which stores address of structure variable โs1โ. So now through โptrโ we can access โRnoโ.
Copying structure variable
There are two different ways to copy one structure variable into another.
Copying each structure member individually struct StudentInfo s1={1,โxyzโ,80.25};
struct StudentInfo s2;
s2.Rno=s1.Rno;
strcpy(s2.Sname,s1.Sname);
s2.Percentage=s1.Percentage;
Copying entire structure variable using assignment operator
struct StudentInfo s1={1,โxyzโ,80.25};
struct StudentInfo s2;
s2=s1;
Here all members of s1 will get copied into members of s2.
Comments
Post a Comment
Please give us feedback through comments