Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Accessing Structure Members

Structure members can be accessed and assigned values in a number of ways. Structure
members have no meaning individually without the structure. In order to assign a value to any
structure member, the member name must be linked with the structure variable using a
dot . operator also called period or member access operator.
For example:
#include<stdio.h>
#include<string.h>

struct Student
{
char name[25];
int age;
char branch[10];
//F for female and M for male
char gender;
};

int main()
{
struct Student s1;

/*
s1 is a variable of Student type and
age is a member of Student
*/
s1.age = 18;
/*
using string function to add name
*/
strcpy(s1.name, "Viraaj");
/*
displaying the stored values
*/
printf("Name of Student 1: %s\n", s1.name);
printf("Age of Student 1: %d\n", s1.age);
return 0;
}

Name of Student 1: Viraaj

Age of Student 1: 18

We can also use scanf() to give values to structure members through terminal.


scanf(" %s ", s1.name);
scanf(" %d ", &s1.age);

Structure Initialization
Like a variable of any other datatype, structure variable can also be initialized at compile time.
struct Patient
{
float height;
int weight;
int age;
};

struct Patient p1 = { 180.75 , 73, 23 }; //initialization


Or
struct Patient p1;
p1.height = 180.75; //initialization of each member separately
p1.weight = 73;
p1.age = 23;

Array of Structure
We can also declare an array of structure variables. in which each element of the array will
represent a structure variable. Example : struct employee emp[5];
The below program defines an array emp of size 5. Each element of the array emp is of
type Employee.
#include<stdio.h>

struct Employee
{
char ename[10];
int sal;
};

struct Employee emp[5];


int i, j;
void ask()
{
for(i = 0; i < 3; i++)
{
printf("\nEnter %dst Employee record:\n", i+1);
printf("\nEmployee name:\t");
scanf("%s", emp[i].ename);
printf("\nEnter Salary:\t");
scanf("%d", &emp[i].sal);
}
printf("\nDisplaying Employee record:\n");
for(i = 0; i < 3; i++)
{
printf("\nEmployee name is %s", emp[i].ename);
printf("\nSlary is %d", emp[i].sal);
}
}
void main()
{
ask();
}
 A structure can be passed to any function from main function or from any sub function.
 Structure definition will be available within the function only.
 It won’t be available to other functions unless it is passed to those functions by value or by address(reference).
 Else, we have to declare structure variable as global variable. That means, structure variable should be declared outside
the main function. So, this structure will be visible to all the functions in a C program.
 Passing structure to a function by value
 Passing structure to a function by address(reference)
 No need to pass a structure – Declare structure variable as global
EXAMPLE PROGRAM – PASSING STRUCTURE TO FUNCTION IN C BY VALUE:
In this program, the whole structure is passed to another function by value. It means the whole structure is passed to another
function with all members and their values. So, this structure can be accessed from called function. This concept is very useful
while writing very big programs in C.

#include <stdio.h>
#include <string.h>
 
struct student
{
            int id;
            char name[20];
            float percentage;
};
 
void func(struct student record);
 
int main()
{
            struct student record;
 
            record.id=1;
            strcpy(record.name, "Raju");
            record.percentage = 86.5;
 
            func(record);
            return 0;
}
 
void func(struct student record)
{
            printf(" Id is: %d \n", record.id);
            printf(" Name is: %s \n", record.name);
            printf(" Percentage is: %f \n", record.percentage);
}

EXAMPLE PROGRAM – PASSING STRUCTURE TO FUNCTION IN C BY ADDRESS:


In this program, the whole structure is passed to another function by address. It means only the address of the structure is passed
to another function. The whole structure is not passed to another function with all members and their values. So, this structure can
be accessed from called function by its address.

6 #include <stdio.h>
7 #include <string.h>
8  
9 struct student
10 {
11            int id;
12            char name[20];
13            float percentage;
14 };
15  
16 void func(struct student *record);
17  
18 int main()
19 {
20           struct student record;
21  
22           record.id=1;
23           strcpy(record.name, "Raju");
24           record.percentage = 86.5;
25  
26           func(&record);
27           return 0;
28 }
29  
30 void func(struct student *record)
{
          printf(" Id is: %d \n", record->id);
          printf(" Name is: %s \n", record->name);
          printf(" Percentage is: %f \n", record->percentage);
}

EXAMPLE PROGRAM TO DECLARE A STRUCTURE VARIABLE AS GLOBAL IN C:


Structure variables also can be declared as global variables as we declare other variables in C. So, When a structure variable is
declared as global, then it is visible to all the functions in a program. In this scenario, we don’t need to pass the structure to any
function separately.

#include <stdio.h>
#include <string.h>
 
5
struct student
6
{
7
            int id;
8
            char name[20];
9
            float percentage;
10
};
11
struct student record; // Global declaration of structure
12
 
13
void structure_demo();
14
 
15
int main()
16
{
17
            record.id=1;
18
            strcpy(record.name, "Raju");
19
            record.percentage = 86.5;
20
 
21
            structure_demo();
22
            return 0;
23
}
24
 
25
void structure_demo()
26
{
27
            printf(" Id is: %d \n", record.id);
28
            printf(" Name is: %s \n", record.name);
29
            printf(" Percentage is: %f \n", record.percentage);
}

C structure can be accessed in 2 ways in a C program. They are,

1. Using normal structure variable


2. Using pointer variable
Dot(.) operator is used to access the data using normal structure variable and arrow (->) is used to access the data using pointer
variable. You have learnt how to access structure data using normal variable in C – Structure topic. So, we are showing here how
to access structure data using pointer variable in below C program.

EXAMPLE PROGRAM FOR C STRUCTURE USING POINTER:


In this program, “record1” is normal structure variable and “ptr” is pointer structure variable. As you know, Dot(.) operator is used to access the data using normal structure variable and arrow(-
>) is used to access data using pointer variable.

1 #include <stdio.h>
2 #include <string.h>
3  
4 struct student
5 {
6      int id;
7      char name[30];
8      float percentage;
9 };
10  
11 int main()
12 {
13      int i;
14      struct student record1 = {1, "Raju", 90.5};
15      struct student *ptr;
16  
17      ptr = &record1;    
18  
19          printf("Records of STUDENT1: \n");
20          printf("  Id is: %d \n", ptr->id);
21          printf("  Name is: %s \n", ptr->name);
22          printf("  Percentage is: %f \n\n", ptr->percentage);
23  
24      return 0;
25 }

You might also like