Unit-2 Structure and Union

● Objective: Understand the user defined data type called structure and unions and its members and variables

● Structures: Introduction, Structure definition, declaration and initialization of structure, accessing structure member, Comparison of structures and Arrays, Arrays of structures variable, array within structure ,concept of Structures within Structures, Structures and functions

● Union: Introduction, union definition, declaration and initialization of union, accessing union member, Comparison of structure and union

● Definition:

↪ Structure is a user defined data type that can store multiple data elements with different data types.

↪ Data elements of structure should be related.

▪ book structure contains all data elements about book like book_id, book_name, book_authorname, book_price, book_totalpages etc.

▪ student structure contains all data elements about student like student_id, student_name, student_mobileno, student_city etc.

● Declaration of Structure:

• struct keyword is used to declare a structure.

•All data elements/members are declared in { }.

● Declaration of Structure variable:

↪ We can declare structure variable in two ways:

  1. At the time of declaration of structure:

struct structure_name ← — — user defined data type

{ data element1; data element2;

— — — data elemetnn;

}structure variable;

2. In main() frunction:

void main() {

structure variable;

}

ex:

void main()

{

struct student s1,s2;

}

● Accessing Structure Members:

▪ We can access structure member using (.) dot operator.

▪ Syntax: structre_variable.structure_member;

▪ ex: b1.id=10

● Initialization/Assignment of Structure variables:

▪ we can assign/initialize structure variable using = (assignment operator).

▪ Syntax:

structure_varialbe.structure_member=value;

struct book b1; ←struct book is used defined data type, b1 is a structure variable.

b1.id=10;

←id is one of the member of book structure.

● Program:

//Structure 1 : WAP to create a structure book. Assign values to its members and display them.

#include<stdio.h>

#include<conio.h>

#include<string.h>

struct book

{

int id;

char name[50];

int price;

char author_name[50];

};

void main()

{

struct book b1; clrscr(); //Assign value b1.id=56;

strcpy(b1.name,”C Programming”); b1.price=260;

strcpy(b1.author_name,”Balaguru”);

//Display

printf(“\nBook id is:%d”,b1.id);

printf(“\nBook name is:%s”,b1.name);

printf(“\nBook proce is:%d”,b1.price);

printf(“\nBook Author name is:%s”,b1.author_name);

getch();

}

//Structure 2: WAP to create a structure book. Scan values to its members and display them.

#include<stdio.h>

#include<conio.h>

struct book

{

int id; char name[50]; int price;

char author_name[50];

};

void main()

{

struct book b1;

clrscr();

//Scan values from user

printf(“\nEnter Book id is:”);

scanf(“%d”,&b1.id);

printf(“\nEnter Book name:”);

scanf(“%s”,b1.name);

printf(“\nEnter Book price:”);

scanf(“%d”,&b1.price);

printf(“\nEnter Book Author name:”);

scanf(“%s”,b1.author_name);

//Display

printf(“\nBook id is:%d”,b1.id);

printf(“\nBook name is:%s”,b1.name);

printf(“\nBook proce is:%d”,b1.price);

printf(“\nBook Author name is:%s”,b1.author_name);

getch();

}

● Comparison of Array and Structure

↪ Array

  1. Array is a collection of data elements with similar data type.

2. Array is a derived data type.

3. There is not any keyword that is used to create an array.

4. Array element is accessed by index value.

5. Array has static memory allocation.

6. Accessing array elements take less time than structure. Int no[5];

7. Every element in an array has same byte size.

8. Ex. : int a[10];

↪ Structure

  1. Structure is a collection of data elements with different data types.

{

int id;

char name[20];

int age;

};

● Arrays of structures variable:

  • Structure is used to store the information of one variable (object). If we need to store information of multiple variables (objects) then we can create an array of structure variable

● Program:

//Array of Structure Variable Student: Create a structure of student that stores information of 5 students.

#include<stdio.h>

#include<conio.h>

struct student

{

int id;

char name[50];

int age;

char address[50];

};

void main()

{

int i;

struct student s1[5];

clrscr();

printf(“\nEnter Student Details:\n”);

for(i=0;i<=4;i++)

{

//Scan values from user

printf(“\nEnter student id :”);

scanf(“%d”,&s1[i].id);

printf(“\nEnter Student name:%s”);

scanf(“%s”,s1[i].name);

printf(“\nEnter Student Age:”);

scanf(“%d”,&s1[i].age);

printf(“\nEnter Student Address:”);

scanf(“%s”,s1[i].address);

}

//Display

printf(“\nDisplay of Student Details:\n”);

for(i=0;i<=4;i++)

{

printf(“\n****************************”);

printf(“\nStudent id :%d”,s1[i].id);

printf(“\nStudent name:%s”,s1[i].name);

printf(“\nStudent Age:%d”,s1[i].age);

printf(“\nStudent Address:%s”,s1[i].address);

}

getch();

}

Software / Website Development