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

Structure and Union_

c programming

Uploaded by

Haf hafeefa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Structure and Union_

c programming

Uploaded by

Haf hafeefa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Structure and Union in C

Structure

Enables to group a finite number of data items of different
data types into a single data object.

Array is a structured data type whose elements are of the
same type.

Structure represents group of elements in which, the elements
can be of different data type.

Members

Individual structure elements

A structure definition involves :

Defining the composition of the structure

Which are the members

What is the data type of each member.

Defining the Composition of Structure

In general, composition of a structure can be
defined as :
struct tag {
type1 member1;
type2 member2;
.......
......
} ; typen membern;
//see the
◆ semicolon
struct is the
◆ keyword.
tag is an identifier recognizes a structure of a particular
composition.

Member declarations are given in a compoud statement.

Members can be simple types, arrays, structures or unions
Defining the Composition of Structure

Example
struct
student {
int
rollno;
char name[30];
}

The entire
; int declaration is considered as a statement.

With the above definition no memory space will be
assigned. mark;

The members of a structure themselves are not variables.

They do not occupy any memory until they are associated
with the structure variables.

Using a variable of student structure, we can store the
Declaring Structure variable

Individual structure varibles can be defined as :
storage-class struct tag variable1, variable2, ...,
variablen


Storage-class is optional

With this variable definition ,memory space will be
allocated.

Each member will be allocated separate memory
space.

Example :-
struct student s;
Declaring Structure variable

It is possible to combine the declaration of the
structure composition with that of the structure
variables.
storage-class struct
tag { type1
member1; type2
member2;
} variable1, variable2, ..., variablen;
.................
Here,
typen storage-class and tag are
optional.
membern; struct {
struct int
char
student { rollno;
char
name[30];
int int
name[30];
} int mark;
rollno; } mark;
Declaring structure
variables
1st Case 2 n d Case
struct Person struct Person
{ {
char name[30]; char name[30];
int age; int age;
char addr[50]; char addr[50];
}; } p1,p2 p[3];
void main()
{
struct Person
p1,p2,p[3];
}
Structure variable - Initialization

Members of structure variable can be initialized.
storage-class struct tag variable = {value1, value2, ...,
valuen};

Example :-
struct student s = {101, “Anil Kumar
P”, 89}; This can also be written instruct {
struct
combined
int {form as
student int
char
rollno; rollno; char
int
name[30];
mark; int
name[30];
marks[4];
} s={101, “Anil Kumar P”, } s={101, “Anil Kumar P”,
89}; 89};
Note:- ANSI C allows only external and static structure varibles to be
initialized.
Accessing the member of a structure

Member of a structure can be accessed using the dot (.)
operator.

Usage is : structure_variable.member
static struct
student { int
int mark;
rollno;
} {101, “Anil Kumar P”,
s = name[30];
char
89}; scanf(“%d”,
printf (“Roll No : %d\n”, &s.rollno);
s.rollno); s.mark=75;
s.nameof=dot
Prioiry “Beena S”; is highest (function call, array
operator
referencing, dot operator have same precedence)
Associativity is Left to Right ( L->R)
Example
Write a program to create a structure Complex with member
variable real and img. Perform addition of two complex
numbers using structure variables.
#include<stdio.h> printf("Enter the real and img part
struct of b:"); scanf("%d
C o m p l ex { int %d",&b.real,&b.img);
real; c.real = a.real +
int i m g ; b.real; c.img =
}; a.img + b.img;
void main() printf("c = %d +
{ %di\
Output:
struct C o m p l e x a,b,c; n",c.real,c.img);
Enter the real and img part of
}
a:10 20
Enter
printf("Enter the real a n d i m g part o f a:"); c = 40the real and img part of b:30 40
scanf("%d%d",&a.real,&a.img); +
60i
Structure containing another structure

Member of a structure can be another structue.
struct date

{ int dd; int mm;


int yyyy;
};
struct student { int rollno;
char name[30];
struct date dob;
int mark;
} s = {101, “Anil Kumar P”,
{20,10,1999}, 89};
Struct salary
{
char name[20];
char department[10];
struct
{
int dearness;
int house_rent;
int city;
}allowance;
}employee;
Example
Write a C p ro g ra m using Structures to read a n d print
data o f n e m p l oye e s (N am e, E m p l o ye e Id a n d Salary).
Array of structure

struct
student { s[0].rollno=100;
int strcpy(s[0].name,”Savi
rollno; er”); s[0].mark[0]=80;
char name[30];
int
mark[4];
} s [50];
typedef – User defined datatype
The typedef feature allows users to define new data-types
that are equivalent to existing data types.
General form of typedef is :
typedef data_type
new_type;
Example:-
typedef int mark; typedef int boolean;
After this definition, we can declare
variables as :
mark m1,m2; boolean found, paid, odd;
typedef struct {
int
} rollno; char Then we can Student
Student;
name[30]; write : s;
Passing structure to function

Entire structure can be passed to a function.

passing by value.
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.
Returning structure from function

A function can return a
structure.
typedef
struct
Student modify(Student
{ int
s)
rno;
{
char
s.rno = 222;
name[30
strcpy(s.name,
];
“Bbbb”);
} Student;
}
return s;
Student
modify(Student);
void main()
Union

Unions, like structures, contain members whose individual
data types may differ from one another.

The members within a union all share the same storage area
within the computer’s memory.

In case of a structure each member is assigned its own
unique storage area.

They are useful for applications involving multiple
members, where values need not be assigned to all of the
members at any one time.

The user must keep track of what type of information is
stored at any given time.

An attempt to access the wrong type of information will
produce meaningless results.
Defining the Composition of union

In general, composition of a union can be
defined as :
union tag {
type1
member1;
type2
member2;
.......
......
typen
membern;
};
Defining the Composition of Union

Example
union
sample {
int
size;
char
colour[12
];
};

With the above
definition no
memory space will
be assigned.
Declaring Union variable

Individual union varibles can be declared as :
storage-class union tag variable1, variable2, ..., variablen

;

Storage-class is optional

With this variable definition memory space will be
allocated.

The memory space allocated to the union varible is
that of the member with highest memory
requirement.
Example :-
Declaring union variable

It is possible to combine the declaration of
the union composition with that of the union
variables.
storage-class union
tag { type1
member1; type2
member2;
} variable1, variable2, ..., variablen;
.................
Here, storage-class and tag are
typen
optional.
membern; union {
union int
sample {
char size;
char
int
} colour[12]; } colour[12];
u; size; u;
Union variable - Initialization

Union variable can be initialized.

Only one member value can be given for
initialization.

Most compilers will accept an initial value for only
one union member, and they will assign this value
to the first member within the union.
According to C99 standard, we can explicitly specify the
member : union sample{
int size;
char colour[12];
} u = { .colour = “White”}
Note:- ANSI C allows only external and static union
varibles to be initialized.
Union variable - Operarions

Most C compilers permit an entire union to be
assigned to another, provided both unions have
the same composition.

These compilers also permit entire unions to be
passed to or from functions (by value), in
accordance with the ANSI standard.
Difference between structure and union

Structure Union
struct keyword is used to define a structure union keyword is used to define a union

Members do not share memory in a Members share the memory space in a


structure. union

Any member can be retrieved at any time in Only one member can be accessed at a time
a structure in a union.

Several members of a structure can be Only one member can be initialized.


initialized at once.

Size of the structure is equal to the sum of Size of the union is equal to the size of the
size of the each member. largest member.
Example - union
#include <stdio.h>
union Job {
float salary;
int
workerNo;
} j;
int main()
{ j.salary =
12.3;
// when j.workerNo
is assigned a
value,
// j.salary will no longer hold 12.3
j.workerNo = 100;
Memory Allocation M e m o r y Allocation U n i o n
Structure

struct E m p l oye e u n i o n E m p l oye e


{ {
char name[50]; / 50 char name[50]; / 50
bytes int id; / 4 bytes int id; / 4
bytes float salary; /4 bytes float salary; /4
bytes bytes
} emp1; / 58 bytes } emp1; / 50 bytes

M e m o r y Allocated = 58 bytes M e m o r y Allocated = 50 bytes

You might also like