Accessing Members of Structure in C

We cannot access members of a structure directly in any expression by specifying their name alone.

There are two ways to access structure members

Using Member Access Operator(.) or Dot Operator

structure_variable.member_name

For Example
We can access the member age of employee structure variable employee_one as:

struct employee
{
    char name[100];
    int age;
    float salary;
    char department[50];
} employee_one = {"Jack", 30, 1234.5, "Sales"};

int age = employee_one.age;

Using Member Structure Pointer Operator or Arrow Operator(->)

Structure pointer operator or Arrow operator is used to access members of structure using pointer variable. When we have pointer to a structure variable, then we can access member variable by using pointer variable followed by an arrow operator and then the name of the member variable.

structure_pointer->member_name;
For Example
struct employee
{
    char name[100];
 int age;
 float salary;
 char department[50];
} employee_one = {"Jack", 30, 1234.5, "Sales"};

struct employee *ptr = &employee_one;
int age = ptr->age;

C Program to print the members of a structure using dot and arrow operators

Accessing Members of Structure in C

#include <stdio.h>
#include <conio.h>
 
struct employee {
    char name[100];
 int age;
 float salary;
 char department[50];
};
 
int main(){
   struct employee employee_one, *ptr;
    
   printf("Enter Name, Age, Salary and Department of Employee\n");
   scanf("%s %d %f %s", &employee_one.name, &employee_one.age,
       &employee_one.salary, &employee_one.department);
 
   /* Printing structure members using dot operator */
   printf("Employee Details\n");
   printf(" Name : %s\n Age : %d\n Salary = %f\n Dept : %s\n", 
       employee_one.name, employee_one.age, employee_one.salary,
       employee_one.department);
        
   /* Printing structure members using arrow operator */
   ptr = &employee_one;
   printf("\nEmployee Details\n");
   printf(" Name : %s\n Age : %d\n Salary = %f\n Dept : %s\n", 
       ptr->name, ptr->age, ptr->salary, ptr->department);
 
   getch();
   return 0;
}

Output

Enter Name, Age, Salary and Department of Employee
Jack 30 1234.5 Sales
Employee Details
 Name : Jack
 Age : 30
 Salary = 1234.500000
 Dept : Sales
 
Employee Details
 Name : Jack
 Age : 30
 Salary = 1234.500000
 Dept : Sales