SlideShare a Scribd company logo
1 of 23
•STRUCTURE
•UNIONS
1. INTRODUCTION
 C’ Supports a constructed data type known
as STRUCTURE, Which is a method of
packing data of different types.
WHAT IS STRUCTURE ?
 A Structure is a convenient tool for handling
a group of logically related data items.
 i.e. It can be used to represent a set of
attributes, such as : student_name , roll_no
A structure defination creates a
formet that may be used to declare
structure variables.
i.e. Consider a book database
consisting of a book name, Author,
Number of pages and price.
We can define a structure to hold this
information as follows:
Struct tag_name
{
data_type member1;
data_type member2;
… …
……… …..
};
1. The template terminated with a semicolon
2. While the entire declarationn considered as a
satement, each member is declared
indepandently for its name and type in a
separate statement inside the teplate.
3. The tag name such as book_bank can be
used to declare structure variables of its
type,letter in the program.
Struct book_bank
{
Char title[20];
Char author[15];
Int pages;
Float price;
};
 The keyword struct declares a structure to
hold the details of four fields, namely
title,author,pages and price.
 These fields are called structure elements or
members and each member belong to
different type of data.
 Book_bank is the name of the structure and
also called ‘STRUCTURE TAG’.
title
author
pages
price
Array of 15
characters
integer
float
Array of 20 characters
 The link between a member and variable is
established using the member operator ‘.’
which is also known as ‘dot operator’ or
‘period operator’.
 i.e.
book1.price
 Is the variable represnting the price of book1
and can be treated like any other ordinary
variable.
 Here is how we would assign values to the
members of book1:
strcpy(book1.title, “COMPUTER”);
strcpy(book1.author, “XYZ”);
book1.pages=250;
book1.price=29.99;
 We can also use scanf to give the values
through keyboard.
 A Structure must be declared as static if it is
to be initialized inside a function.
 main()
{
static struct
{
int weight;
float height;
}
student = (60,180.75);
…..
}
 If there are fewer initialization than that of
member variables in the structure. The
remaining member variables are initialized to
zero.
 i.e. if we don’t know the number of pages in
book:
struct book b1={“let us
C”,”kanetkar”,0,150.50};
 We use structure todescribe the format of a
number related variables.
 In such cases, we may declare array of
structures,each element of an array represent
a structure variable
 i.e. struct class student[100]
 This defines an array called student , that
consists of 100 element.
 Each element is defined to be of the type
struct class.
 An array of structure is stored inside the
memory in the same way as a multi-
dimensional array.
 C permits the use of array as a structure
members.
 We can use single or multi-dimensional array
of type int or float.
 i.e. Struct marks
{
int number;
float subject[3];
} student[2];
 The main philosophy of c language is the use
of functions. C supports the passing of
structure values as arguments to function.
 There are 3 methods by which the values of a
structure can be transfferd from one function
to another:
1. To pass each member of the structure as an
argument of function call.
2. Passing of a copy of the entire structure to
the called function
3. Pointers to pass the structure as an
argument.
 General formet of sending a copy of a
structure to thr called function:
data_type function name(st_name)
srtuct_type st_name;
{
…
return(expression);
}
 Unions are a concept borrowed from
structures and therefore follow the same
syntax as structures.
 Major diffferance in terms of Storage:
In structures each member has its
own storage location
In unions all members use the same
location
 Union may contain many members of
different type but can handle only one
member at a time.
 It can be declared using keyword union as
follows:
 union item
{
int m;
float x;
char c;
} code;
 Union union_name
{
data_type member1;
data_type mrmber2;
… … .. …..
} var1, var2, .. ;
Union book;
{
char title[15];
char *author;
int pages;
float price;
} b1, b2, b3;
 A union variable can be assigned to another
union variable.
 Address of the union variable is obtained
using the address of ‘&’ operator.
 It is to pass a union to function and a
function can return a union.
 We can use pointer to unions and within
unions.
 All members share the same storage area in
computers memory.

More Related Content

What's hot (20)

Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Function in c
Function in cFunction in c
Function in c
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Structure c
Structure cStructure c
Structure c
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Function in C
Function in CFunction in C
Function in C
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Strings
StringsStrings
Strings
 
C++ string
C++ stringC++ string
C++ string
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
 
Array in c
Array in cArray in c
Array in c
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 

Similar to C Structures and Unions

Similar to C Structures and Unions (20)

structure and union1.pdf
structure and union1.pdfstructure and union1.pdf
structure and union1.pdf
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Structure & union
Structure & unionStructure & union
Structure & union
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and Typedef
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
 
Str
StrStr
Str
 
Definition, Declaration of Structures in C.pptx
Definition, Declaration of Structures in C.pptxDefinition, Declaration of Structures in C.pptx
Definition, Declaration of Structures in C.pptx
 

Recently uploaded

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...Call girls in Ahmedabad High profile
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 

Recently uploaded (20)

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
High Profile Call Girls Dahisar Arpita 9907093804 Independent Escort Service ...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 

C Structures and Unions

  • 2. 1. INTRODUCTION  C’ Supports a constructed data type known as STRUCTURE, Which is a method of packing data of different types. WHAT IS STRUCTURE ?  A Structure is a convenient tool for handling a group of logically related data items.  i.e. It can be used to represent a set of attributes, such as : student_name , roll_no
  • 3. A structure defination creates a formet that may be used to declare structure variables. i.e. Consider a book database consisting of a book name, Author, Number of pages and price. We can define a structure to hold this information as follows:
  • 4. Struct tag_name { data_type member1; data_type member2; … … ……… ….. };
  • 5. 1. The template terminated with a semicolon 2. While the entire declarationn considered as a satement, each member is declared indepandently for its name and type in a separate statement inside the teplate. 3. The tag name such as book_bank can be used to declare structure variables of its type,letter in the program.
  • 6. Struct book_bank { Char title[20]; Char author[15]; Int pages; Float price; };
  • 7.  The keyword struct declares a structure to hold the details of four fields, namely title,author,pages and price.  These fields are called structure elements or members and each member belong to different type of data.  Book_bank is the name of the structure and also called ‘STRUCTURE TAG’.
  • 9.  The link between a member and variable is established using the member operator ‘.’ which is also known as ‘dot operator’ or ‘period operator’.  i.e. book1.price  Is the variable represnting the price of book1 and can be treated like any other ordinary variable.
  • 10.  Here is how we would assign values to the members of book1: strcpy(book1.title, “COMPUTER”); strcpy(book1.author, “XYZ”); book1.pages=250; book1.price=29.99;  We can also use scanf to give the values through keyboard.
  • 11.  A Structure must be declared as static if it is to be initialized inside a function.  main() { static struct { int weight; float height; } student = (60,180.75); ….. }
  • 12.  If there are fewer initialization than that of member variables in the structure. The remaining member variables are initialized to zero.  i.e. if we don’t know the number of pages in book: struct book b1={“let us C”,”kanetkar”,0,150.50};
  • 13.  We use structure todescribe the format of a number related variables.  In such cases, we may declare array of structures,each element of an array represent a structure variable  i.e. struct class student[100]  This defines an array called student , that consists of 100 element.
  • 14.  Each element is defined to be of the type struct class.  An array of structure is stored inside the memory in the same way as a multi- dimensional array.
  • 15.  C permits the use of array as a structure members.  We can use single or multi-dimensional array of type int or float.  i.e. Struct marks { int number; float subject[3]; } student[2];
  • 16.  The main philosophy of c language is the use of functions. C supports the passing of structure values as arguments to function.  There are 3 methods by which the values of a structure can be transfferd from one function to another: 1. To pass each member of the structure as an argument of function call.
  • 17. 2. Passing of a copy of the entire structure to the called function 3. Pointers to pass the structure as an argument.  General formet of sending a copy of a structure to thr called function: data_type function name(st_name) srtuct_type st_name; { … return(expression); }
  • 18.  Unions are a concept borrowed from structures and therefore follow the same syntax as structures.  Major diffferance in terms of Storage: In structures each member has its own storage location In unions all members use the same location
  • 19.  Union may contain many members of different type but can handle only one member at a time.  It can be declared using keyword union as follows:  union item { int m; float x; char c; } code;
  • 20.  Union union_name { data_type member1; data_type mrmber2; … … .. ….. } var1, var2, .. ;
  • 21. Union book; { char title[15]; char *author; int pages; float price; } b1, b2, b3;
  • 22.  A union variable can be assigned to another union variable.  Address of the union variable is obtained using the address of ‘&’ operator.  It is to pass a union to function and a function can return a union.  We can use pointer to unions and within unions.
  • 23.  All members share the same storage area in computers memory.