Access structure members

Access structure members

for accessing any member of a structure variable, We use the dot(.) operator which is also known as membership operator, The syntax to access the members of structure

structvariable.member

in the above syntax on the left side of the .(dot) operator there should be variable of structure type and on the other hand (right hand side) of the .(dot) operator there should be name of member of the structure. lets us take the example to access member of structure

struct Employee
{
char name[15];
float salary;
int empid;
}
struct Employee emp1;

name member of emp1 is given by emp1.name
salary member of emp1 is given by emp1.salary
empid member of emp1 is given by emp1.empid

/ * program to print the values of structure members */
#include<stdio.h>
#include<conio.h>
struct
Employee
{
char name[15];
int empid;
float salary;
};
int main()
{
struct Employee emp1={"Nitish",2092,2000};
struct Employee emp2,emp3;
strcpy=(emp2.name,"gaurav");
emp2.empid=890;
emp2.salary=10000;
printf("Enter the name Empid and salary: ");
scanf("%s %d %f \n",&emp3.name,&emp3.empid,&emp3.salary);
printf("emp1: %s %d %f \n",emp1.name,emp1.empid,emp1.salary);
printf("emp2: %s %d %f \n",emp2.name,emp2.empid,emp2.salary);
printf("emp3: %s %d %f \n",emp3.name,emp3.empid,emp3.salary);
return 0;
}

output:
Enter the name Empid and salary: Nitish 098 2000
emp1: Nitish 2092 2000
emp2: Gaurav 890 10000
emp3: Nitish 098 2000

In this program we declare three variables of type struct Employee. The first variable emp1 has initialize value of name, epmid and salary and second variable emp2 also initialize value of members and the third variable emp3 has input the member values from the user.

Assignment of structure variable

/* program to assign values of one structure to another structure variable */
#include<stdio.h>
struct
Employee
{
char name[20];
int empid;
float salary;
};
int main()
{
struct Employee emp1={"Rajesh",9898,1000};
struct Employee emp2;
emp2=emp1;
printf("emp1: %s %d %f \n",emp1.name,emp1.empid,emp1.salary);
printf("emp2: %s %d %f",emp2.name,emp2.empid,emp2.salary);
return 0;
}

output:
emp1: Rajesh 9898 1000
emp2: Rajesh 9898 1000

Storage allocation and size of structure