Chapter 11 Structures
Chapter 11 Structures
• struct Employee {
• int day1, day2, day3, day4, day5, day6, day7;
• };
C program to demonstrate the array within structures
include <string.h>
#include <stdio.h>// Defining array within structure
struct Employee { // character array to store name of the employee
char Name[20];
int employeeID;
// integer array to maintain the record of attendanc eof
// the employee
int WeekAttendence[7];
};int main()
{ // defining structure of type Employee
struct Employee emp; // adding data
emp.employeeID = 1;
strcpy(emp.Name, "Rohit");
int week;
for (week = 0; week < 7; week++) {
int attendence;
emp.WeekAttendence[week] = week;
}
printf("\n"); // printing the data
printf("Emplyee ID: %d - Employee Name: %s\n",
emp.employeeID, emp.Name);
printf("Attendence\n");
for (week = 0; week < 7; week++) {
printf("%d ", emp.WeekAttendence[week]);
}
return 0;
}
Array of structures
• In order to declare or initialize a set of structure variable at a time in a
single statement rather than one-by-one, C language permits an
‘array of structures’, where each element the array represents a
different structure variable.
• EX: struct marks student[3] = {
{“Mani,{60,95,85},0},
{“Srikanth”,{80, 80,60},0},
{“Bhagya”,{70,60,40},0}
};
Passing structure to functions
• 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.
#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);
}
UNION
• The Union is a user-defined data type in C language that can contain elements of the different
data types just like structure. But unlike structures, all the members in the C union are stored in
the same memory location. Due to this, only one member can store data at the given instance.
• Syntax of Union in C
In this part, we only declare the template of the union, i.e., we only declare the members’ names
and data types along with the name of the union. No memory is allocated to the union in the
declaration.
union union_name {
datatype member1;
datatype member2;
...
};
Declaration of union variables
• Once the union datatype is created , variables can be declared as
shown below:
• Union student
{
Int age;
Char sex;
Float percent;
} u1,u2;
Union student s1,s2,s3;
Accessing members of union
• We can access the members of a union by using the ( . ) dot operator
just like structures.
var1.member1;
Ex:
S1.age = 10;
S1.gender = ‘m’,
S1.percent = 78.56
Difference between structure and
union
Advantages of structures
• Heterogeneous collection of data items: structure allows us to create user
defined data-type which can store items with different data types.
• Reduced complexity
• increased productivity: structure in C eliminates lots of burden while
dealing with records which contain heterogeneous data items, thereby
increasing productivity.
• Maintainability of code: using structure, we represent complex records by
using a single name, which makes code maintainability like a breeze.
• Enhanced code readability: code readability is crucial for larger projects.
Using structure code looks user friendly which in turn helps to maintain the
project.
Difference between arrays and
structures