SlideShare a Scribd company logo
•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)

PPTX
stack & queue
manju rani
 
PDF
What is Serialization in Java? | Java Tutorial | Edureka
Edureka!
 
PPTX
Characteristics of OOPS
abhishek kumar
 
PPTX
Stack data structure
Tech_MX
 
PPTX
Type casting in java
Farooq Baloch
 
PPTX
single linked list
Sathasivam Rangasamy
 
PPTX
Pointer in c
Imamul Kadir
 
PPT
Enumerated data types in C
Arpana shree
 
PPTX
Strings in c++
Neeru Mittal
 
PPTX
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
PPTX
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 
PPTX
Function overloading and overriding
Rajab Ali
 
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PPTX
inline function
imran khan
 
PPTX
Constructors in C++
RubaNagarajan
 
PPTX
Functions in c language
tanmaymodi4
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
Graphs in data structure
hamza javed
 
PPT
Difference between stack and queue
Pulkitmodi1998
 
stack & queue
manju rani
 
What is Serialization in Java? | Java Tutorial | Edureka
Edureka!
 
Characteristics of OOPS
abhishek kumar
 
Stack data structure
Tech_MX
 
Type casting in java
Farooq Baloch
 
single linked list
Sathasivam Rangasamy
 
Pointer in c
Imamul Kadir
 
Enumerated data types in C
Arpana shree
 
Strings in c++
Neeru Mittal
 
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 
Function overloading and overriding
Rajab Ali
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
inline function
imran khan
 
Constructors in C++
RubaNagarajan
 
Functions in c language
tanmaymodi4
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
classes and objects in C++
HalaiHansaika
 
Graphs in data structure
hamza javed
 
Difference between stack and queue
Pulkitmodi1998
 

Viewers also liked (18)

PDF
Ppl for students unit 1,2 and 3
Akshay Nagpurkar
 
PDF
Principles of programming languages. Detail notes
VIKAS SINGH BHADOURIA
 
PPT
Unit 2 Principles of Programming Languages
Vasavi College of Engg
 
PPT
Unit 3 principles of programming language
Vasavi College of Engg
 
PPTX
Unit1 principle of programming language
Vasavi College of Engg
 
PPTX
Standing waves
degaa
 
PDF
Beti hai to kal hai- july 2016
Withs Technosolutions
 
PDF
TOOBEEZ Product Guide
Chris Stummer
 
PDF
Vipra today november 16
Withs Technosolutions
 
PDF
Omar Hanafy (1)
omar hanafy
 
DOCX
CV NEW NEW (1)
Patra Nona
 
ODP
NoSQL as Not Only SQL
Stefanie Janine Stölting
 
PDF
Survey of Instruction Hotel Management Prof Godbe
Bill Godbe
 
PDF
Marcella Marletta - Vigilanza, sorveglianza del mercato e contraffazione
Marcella Marletta
 
PDF
Good Manufacturing Practices Awareness Posters
Sachin Sarkhot
 
PDF
LawGeex gives you back your TIME.
Manson Ho
 
Ppl for students unit 1,2 and 3
Akshay Nagpurkar
 
Principles of programming languages. Detail notes
VIKAS SINGH BHADOURIA
 
Unit 2 Principles of Programming Languages
Vasavi College of Engg
 
Unit 3 principles of programming language
Vasavi College of Engg
 
Unit1 principle of programming language
Vasavi College of Engg
 
Standing waves
degaa
 
Beti hai to kal hai- july 2016
Withs Technosolutions
 
TOOBEEZ Product Guide
Chris Stummer
 
Vipra today november 16
Withs Technosolutions
 
Omar Hanafy (1)
omar hanafy
 
CV NEW NEW (1)
Patra Nona
 
NoSQL as Not Only SQL
Stefanie Janine Stölting
 
Survey of Instruction Hotel Management Prof Godbe
Bill Godbe
 
Marcella Marletta - Vigilanza, sorveglianza del mercato e contraffazione
Marcella Marletta
 
Good Manufacturing Practices Awareness Posters
Sachin Sarkhot
 
LawGeex gives you back your TIME.
Manson Ho
 
Ad

Similar to CPU : Structures And Unions (20)

PDF
structure and union1.pdf
HMCollegeInfo
 
PPTX
Unit-V.pptx
Mehul Desai
 
PPTX
Structure & union
lalithambiga kamaraj
 
PPT
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
PDF
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PPT
structure and union from C programming Language
AvrajeetGhosh
 
PPT
structure and union from c programming language.ppt
AvrajeetGhosh
 
PPTX
U5 SPC.pptx
thenmozhip8
 
PPTX
Structures
DrJasmineBeulahG
 
DOCX
C programming structures & union
Bathshebaparimala
 
PPTX
Unit 9. Structure and Unions
Ashim Lamichhane
 
DOCX
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Rajeshkumar Reddy
 
PPTX
Unit 1_ADC.pptx
Itsbrokenstatus
 
PDF
Lk module4 structures
Krishna Nanda
 
PDF
Principals of Programming in CModule -5.pdf
anilcsbs
 
PDF
slideset 7 structure and union (1).pdf
HimanshuKansal22
 
PPTX
Programming for problem solving-II(UNIT-2).pptx
prathima304
 
PPTX
detail structure presentation of problem solving
talencorconsultancy
 
structure and union1.pdf
HMCollegeInfo
 
Unit-V.pptx
Mehul Desai
 
Structure & union
lalithambiga kamaraj
 
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
structure and union from C programming Language
AvrajeetGhosh
 
structure and union from c programming language.ppt
AvrajeetGhosh
 
U5 SPC.pptx
thenmozhip8
 
Structures
DrJasmineBeulahG
 
C programming structures & union
Bathshebaparimala
 
Unit 9. Structure and Unions
Ashim Lamichhane
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
Rajeshkumar Reddy
 
Unit 1_ADC.pptx
Itsbrokenstatus
 
Lk module4 structures
Krishna Nanda
 
Principals of Programming in CModule -5.pdf
anilcsbs
 
slideset 7 structure and union (1).pdf
HimanshuKansal22
 
Programming for problem solving-II(UNIT-2).pptx
prathima304
 
detail structure presentation of problem solving
talencorconsultancy
 
Ad

Recently uploaded (20)

PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PDF
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PPTX
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
PPTX
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
NTPC PATRATU Summer internship report.pdf
hemant03701
 
PPTX
Fundamentals of Quantitative Design and Analysis.pptx
aliali240367
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
Module - 5 Machine Learning-22ISE62.pdf
Dr. Shivashankar
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Explore USA’s Best Structural And Non Structural Steel Detailing
Silicon Engineering Consultants LLC
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
UNIT 1 - INTRODUCTION TO AI and AI tools and basic concept
gokuld13012005
 
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
NTPC PATRATU Summer internship report.pdf
hemant03701
 
Fundamentals of Quantitative Design and Analysis.pptx
aliali240367
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
template.pptxr4t5y67yrttttttttttttttttttttttttttttttttttt
SithamparanaathanPir
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 

CPU : 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.