0% found this document useful (0 votes)
52 views

"Structure and Union": Presented By: Sunil Subedi Roshan Ray Chaudary Ushan Buddhacharya

The document discusses different aspects of structures in C programming language. It explains what structures are, how to define structures, declare structure variables, access structure members, pass structures to functions, create arrays of structures, and have structures within structures. Some key points include that structures group related data types together, members are accessed using dot operator, structures can be passed to functions as arguments, and structures allow nesting other structures for complex data types.

Uploaded by

Samir Shakya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

"Structure and Union": Presented By: Sunil Subedi Roshan Ray Chaudary Ushan Buddhacharya

The document discusses different aspects of structures in C programming language. It explains what structures are, how to define structures, declare structure variables, access structure members, pass structures to functions, create arrays of structures, and have structures within structures. Some key points include that structures group related data types together, members are accessed using dot operator, structures can be passed to functions as arguments, and structures allow nesting other structures for complex data types.

Uploaded by

Samir Shakya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

“STRUCTURE AND

UNION”

Presented By:
• Sunil Subedi
• Roshan Ray Chaudary
• Ushan Buddhacharya
Let’s Start With
Structure
“STRUCTURE”
Quick Notes

 Structures in C are used to encapsulate, or group together


different data into one object.

 Can be compared to record.

 Helps to organize the data’s in a meaningful way.

 The size of a structure is equal to the total size of the


members in the structure.
“GENERAL FORM OF
STRUCTURE”
struct tag_name
{
data_type member 1;
data_type member 2;
………….. ………….
………….. ………….
data_type member n;
};
“STRUCTURE DEFINITION”
struct student Structure tag Elements of
structure
{
char name[25]; //to store the name
char add[25]; //to store address

}; //terminated after completion of definition


void main()
{ v a r ia b le
s tr u c t
struct student example;

Remember that the members of a structure themselves are not


variables. They do not occupy any memory until they are associated
with the structure variable such as example.
::::::::::Note that the above declaration has not declared any variables.
It simply describes a format called template to represent information as
shown below:-

struct student

name Array of 25 characters

add Array of 25 characters


“GIVING VALUE TO MEMBERS”
 The members themselves are not variables.
 To make them meaningful members they must be linked to the structure
variables.
 ‘.’ (“dot operator” or “period operator”) is used to link them
together.
For example:- example.name struct student
Direct initialization {
example.name=“Ram”; char name[25];
example.add=“NewRoad”;
char add[25];
Or, structure variable helps to
}; link with the structure
struct student example={“Ram”,”NewRoad”}; members.
void main()
{
Taking input
struct student example;
scanf(“%s”,example.name);
scanf(“%s”,example.add);
As we know this much….

Lets see structure in action.....


/*program to take name & address of student input and show them*/

#include<stdio.h>
#include<conio.h>
struct student //structure prototype and definition
{
char name[25];
char add[25];
}example;
Optional step
void main()
{
struct student example; //where example is a structure variable
clrscr();
printf(“\n Enter name and address : ”);
scanf(“%s%s”,example.name,example.add); //see how value is stored

printf(“Your name is %s and you are from %s”,example.name,example.add); //see


how value is displayed
getch(); //hold the screen
}
“Output”

Your name is Ram and you are from NewRoad.

[[where Ram and NewRoad are input given by the user….]]


“ARRAYS OF STRUCTURES”
 Since structures are data types that are especially useful for creating
collection items, why not make a collection of them using an array?

 struct student //structure prototype and defiition


 {
 char name[25]; //to store the name
 char add[25]; //to store the address
 }example[100];
 void main()
Optional
 {
 struct student example[100]; //array of structure
 }
 This declares “example” as an array of three elements i.e.
1. example[0]
2. example[1]
3. example[2]
4. example[3]
5. example[4]
6. example[5]
7. example[6]
8. example[7]
…….up to……..
 example[99]
/*program to take name and address of 3 students as input and display them*/
#include<stdio.h>
#include<conio.h>
struct student
{
char name[25];
char add[25];
}std[3];

void main() Optional process


{
int i;
struct student std[3];

for(i=0;i<3;i++)
{
printf(“\n Enter students name and address:”)
scanf(“%s%s”,std[i].name,std[i].add);
}

for(i=0;i<3;i++)
{
printf(“\n Your name is %s and you are from %s”,std[i].name, std[i].add);
}
getch();
}
“Output”
Your name is Ram and you are from Thamel
Your name is Hari and you are from Bhaktapur
Your name is Shyam and you are from Ason

((where Ram and Thamel are 1st inputs,


Hari and Bhaktapur are 2nd inputs and
Shyam and Ason are 3rd inputs))
“ARRAYS WITHIN STRUCTURES”
 Lets write a program that will input:
1. Name,
2. Address,
3. Roll number
4. Total marks of a student in 5 subjects.
 ((And display it in ordered form))

 For this it is ridiculous to make separate variables to store separate


marks.

 So here we can use array…..marks[5];


/*program that will input the name, address, Roll number, total marks of a student in 5
subjects and display it in ordered form*/

#include<stdio.h>
#include<conio.h>
struct student
{
char name[25];
int roll_no;
int marks[5]; //array to store marks in 5 subjects
}a;
void main()
{
int i, total=0;
clrscr();
printf(“\n Enter name and roll number of the student : ”);
scanf(“%s %d”,a.name,&a.roll_no);
//to record marks
printf(“\n Enter the marks in 5 subjects : ”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a.marks[i]); //where the value of i varies in different section of loops
}
for(i=0;i<5;i++)
{
total+=a.marks[i];
}
printf(“\n Name:%s \n Roll No:%d \n Total Marks:%d”,a.name,a.roll_no,total);
getch();
}
“Output”
Name : Ram
Roll No : 7
Total Marks : 85

Where “Ram”, ‘7’ are input for name and roll number
And
((20,15,20,18,12)) are marks input.
“STRUCTURES AND
FUNCTIONS”
 The main philosophy of the C language is to use functions.

 Let us now learn to pass a structure to a function.  As an example let us use


a function that prints members of the structure passed to it:

1. void print_data(struct student a) struct student


2. { {
char name[25];
3. printf(“Name: %s”,a.name);
char add[25];
4. printf(“Address: %s”,a.add); int roll;
5. printf(“Roll Number: %d”,a.roll); }a;
6. }
/*program to demonstrate arguments but no return value using function and
structure*/
#include<stdio.h>
#include<conio.h>
struct student
{
char name[25];
char add[25];
int roll;
}a;
void print_data(struct student);
void main()
{
clrscr();
printf(“”\n Enter name ,address and roll number);
scanf(“%s%s%d”,a.name,a.add,&a.roll);
print_data(a);
getch();
}
void print_data(struct student a)
{
printf(“\nName:%s \n Address:%s \n RollNo:%d”,a.name,a.add,a.roll);
}
“Output”
Name : Ram
Address : NewRoad
Roll No : 7

Where “Ram”, “NewRoad” and ‘7’ are input for name,


address and roll number.
“STRUCTURES WITHIN
STRUCTURES”
 Structure within a structure means nesting of structure.
 We can group together the similar items in one separate structure within a
structure.

 Consider an example below:-


1. struct student
2. {
3. char name[25];
4. char faculty[10];
5. struct
6. {
7. int marks;
8. float per;
9. char grade;
10. }scores; //nested structure tag or name
11. }a; //main structure tag or name
“ACCESSING NESTED
MEMBERS”
 Accessing marks Field : a.scores.marks
 Accessing per Field : a.scores.per
 Accessing grade Field : a.scores.per

 Examples:
1. struct student
 scanf(“%d”,&a.scores.marks); 2. {
 printf(“%d”,a.scores.marks); 3. char name[25];
4. char faculty[10];
5. struct
6. {
7. int marks;
8. float per;
9. char grade;
10. }scores; //nested structure tag or
name
11. }a; //main structure tag or name
/*program to demonstrate a simple nested structure program*/

#include<stdio.h>
#include<conio.h>
struct Employee
{
char name[20];
int id;
float salary;
struct Date
{
int date;
int month;
int year;
}doj;
}emp = {“Ram",1000,1000.50,{22,6,1990}};

void main()
{
printf("\nEmployee Name : %s",emp.ename);
printf("\nEmployee ID : %d",emp.id);
printf("\nEmployee Salary : %f",emp.salary);
printf("\nEmployee DOJ : %d/%d/%d", \ emp.doj.date,emp.doj.month,emp.doj.year);
getch();
}
“Output”
Name : Ram
ID : 1000
Salary : 1000.50
DOJ : 22/6/1990

Where “Ram” , “1000” , “100.50” were value of name, id


and salary
And
“22” , “6” , “1990” were values of date, month and year.
“UNIONS”
 The size of a union is equal to the size of its member with the
greater size.

 Only one element in the union may have a value set at any given
time.

 Values are not assigned to all the members at any one time.

 The members that compose a union share the common storage


area within the memory.
“GENERAL FORM”
1. union tag_name
2. {
3. data_type member 1;
4. data_type member 2;
5. ………….. ………….
6. ………….. ………….
7. data_type member n;
8. }[one or more union variables] ;
“Main Difference Between Structure and Union”

union item struct item


{ {
int m; int m;
float x; float x;
char c; char c;
}; };

here the size of the structure ‘item’ is equal here the size of the structure ‘item’ is equal to
to the size of its greatest member. the sum of total size of its members.

sizeof(struct item)=sizeof(x) sizeof(struct item)=sizeof(m,x,c)


=4bytes. =7bytes.

Storage of 4 bytes is created Storage of 7 bytes is created

1000 1001 1002 1003 1000 1001 1002


1000 c m
c
m 1003 1004 1005 1006
x x
An example of

Union is given in

the next slide….


/*program to demonstrate how union works*/
#include<stdio.h>
#include<conio.h>
union example
{
int i;
float f;
char c[20];
“Output”
};
void main( )
{
union example data;
data.i = 10; data.i = 1917853763
data.f = 220.5;
data.c = “Structure”;
data.f = 41223605808.000000
printf( “\n data.i : %d", data.i); data.c = Structure
printf( “\n data.f : %f", data.f);
printf( “\n data.c : %s", data.c);
getch();
}
/*program to demonstrate how union works*/

#include<stdio.h>
#include<conio.h>
union example
{
int i;
float f;

“Output”
char c[20];
};
void main( )
{
union example data;
data.i = 10;
data.f = 220.5; data.i = 1917853763
data.c = “Structure”; data.f = 41223605808.000000
printf( “\n data.i : %d", data.i);
printf( “\n data.f : %f", data.f);
data.c = Structure
printf( “\n data.c : %s", data.c);
getch();
}

Here, we can see that values of i and f members of union got corrupted because final value
assigned to the variable has occupied the memory location and this is the reason that the
value of c member is getting printed very well.
Now let's look into the same example once again where we will use one
variable at a time which is the main purpose of having union:

#include<stdio.h>
#include<conio.h>
union example

“Output”
{
int i;
float f;
char c[20];
};
void main( ) data.i : 10
{
union example data;
data.f : 220.500000
data.i = 10; data.c:Structure
printf( “\n data.i : %d", data.i);
data.f = 220.5;
printf( “\n data.f : %f", data.f);
data.c = “Structure”;
printf( “\n data.c : %s", data.c);
getch();
}

Here, all the members are getting printed very well because one member is being used
at a time.
Thank You…..

You might also like