how to access structure members in c
C programming Tutorials

How to Access Structure Members in C ?

How to Access Structure Members in C ?

Structure in C Programming is an important construct which is used to the information of different types of variable or data elements.

We can access the structure members (means read and assign the value of structure member)  using dot . operator in c programming.

1.Structure members are accessed using dot [.] operator.
2. (.) is called as “Structure member Operator”.
3. Use this Operator in between “Structure name” & “member name”

Program to Access the Structure Members  in C is given below

#include<stdio.h>

#include<string.h>

#include<conio.h>

// create struct with person1 variable

struct Person

{

char name[50];

int empid;

float salary;

} person1;

void main( )

{

// assign value to name of person1

strcpy(person1.name, “Satish”);

// assign values to other person1 variables

person1.empid = 7711;

person1. salary = 2500;

clrscr( );

// print struct variables

printf(“Name: %s\n”, person1.name);

printf(“Empid.: %d\n”, person1.empid);

printf(“Salary: %.2f”, person1.salary);

getch( );

}

Output

accessing structure members

 

 

 

Explanation

In this program we have declared a structure person having three members name , empid and salary and one structure variable person1. All structure members are associated with this structure variable person1.

We have assigned the value of members in the program and then accessing / printing the assigned value using dot . operator.

Structure member can not be initialized at the time of declaration the reason for above error is simple, when a data type is declared, no memory is allocated for it. Memory is allocated only when variables are created.

Structure members can be initialized using curly braces ‘{}’. For example, the following is a valid initialization.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *