SlideShare uma empresa Scribd logo
1 de 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.

Mais conteúdo relacionado

Mais procurados (20)

Structure in c
Structure in cStructure in c
Structure in c
 
structure and union
structure and unionstructure and union
structure and union
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
ppt on pointers
ppt on pointersppt on pointers
ppt on pointers
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
File in C language
File in C languageFile in C language
File in C language
 
Python variables and data types.pptx
Python variables and data types.pptxPython variables and data types.pptx
Python variables and data types.pptx
 
Functions in c
Functions in cFunctions in c
Functions in c
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Structure c
Structure cStructure c
Structure c
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Structure and union
Structure and unionStructure and union
Structure and union
 
Function in c program
Function in c programFunction in c program
Function in c program
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and Typedef
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
 
Function in c
Function in cFunction in c
Function in c
 
Computer Science:Pointers in C
Computer Science:Pointers in CComputer Science:Pointers in C
Computer Science:Pointers in C
 

Destaque

Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3Akshay Nagpurkar
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notesVIKAS SINGH BHADOURIA
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesVasavi College of Engg
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming languageVasavi College of Engg
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming languageVasavi College of Engg
 
Standing waves
Standing wavesStanding waves
Standing wavesdegaa
 
TOOBEEZ Product Guide
TOOBEEZ Product GuideTOOBEEZ Product Guide
TOOBEEZ Product GuideChris Stummer
 
CV NEW NEW (1)
CV NEW NEW (1)CV NEW NEW (1)
CV NEW NEW (1)Patra Nona
 
Survey of Instruction Hotel Management Prof Godbe
Survey of Instruction Hotel Management Prof GodbeSurvey of Instruction Hotel Management Prof Godbe
Survey of Instruction Hotel Management Prof GodbeBill Godbe
 
Marcella Marletta - Vigilanza, sorveglianza del mercato e contraffazione
Marcella Marletta - Vigilanza, sorveglianza del mercato e contraffazioneMarcella Marletta - Vigilanza, sorveglianza del mercato e contraffazione
Marcella Marletta - Vigilanza, sorveglianza del mercato e contraffazioneMarcella Marletta
 
Good Manufacturing Practices Awareness Posters
Good Manufacturing Practices Awareness PostersGood Manufacturing Practices Awareness Posters
Good Manufacturing Practices Awareness PostersSachin Sarkhot
 
LawGeex gives you back your TIME.
LawGeex gives you back your TIME.LawGeex gives you back your TIME.
LawGeex gives you back your TIME.Manson Ho
 

Destaque (18)

Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3Ppl for students unit 1,2 and 3
Ppl for students unit 1,2 and 3
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notes
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 2 Principles of Programming Languages
Unit 2 Principles of Programming LanguagesUnit 2 Principles of Programming Languages
Unit 2 Principles of Programming Languages
 
Unit 3 principles of programming language
Unit 3 principles of programming languageUnit 3 principles of programming language
Unit 3 principles of programming language
 
Unit1 principle of programming language
Unit1 principle of programming languageUnit1 principle of programming language
Unit1 principle of programming language
 
Standing waves
Standing wavesStanding waves
Standing waves
 
Beti hai to kal hai- july 2016
Beti hai to kal hai- july 2016Beti hai to kal hai- july 2016
Beti hai to kal hai- july 2016
 
TOOBEEZ Product Guide
TOOBEEZ Product GuideTOOBEEZ Product Guide
TOOBEEZ Product Guide
 
Vipra today november 16
Vipra today november 16Vipra today november 16
Vipra today november 16
 
Omar Hanafy (1)
Omar Hanafy (1)Omar Hanafy (1)
Omar Hanafy (1)
 
CV NEW NEW (1)
CV NEW NEW (1)CV NEW NEW (1)
CV NEW NEW (1)
 
NoSQL as Not Only SQL
NoSQL as Not Only SQLNoSQL as Not Only SQL
NoSQL as Not Only SQL
 
Survey of Instruction Hotel Management Prof Godbe
Survey of Instruction Hotel Management Prof GodbeSurvey of Instruction Hotel Management Prof Godbe
Survey of Instruction Hotel Management Prof Godbe
 
Marcella Marletta - Vigilanza, sorveglianza del mercato e contraffazione
Marcella Marletta - Vigilanza, sorveglianza del mercato e contraffazioneMarcella Marletta - Vigilanza, sorveglianza del mercato e contraffazione
Marcella Marletta - Vigilanza, sorveglianza del mercato e contraffazione
 
Good Manufacturing Practices Awareness Posters
Good Manufacturing Practices Awareness PostersGood Manufacturing Practices Awareness Posters
Good Manufacturing Practices Awareness Posters
 
LawGeex gives you back your TIME.
LawGeex gives you back your TIME.LawGeex gives you back your TIME.
LawGeex gives you back your TIME.
 

Semelhante a CPU : Structures And Unions

Semelhante a CPU : 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
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
 
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
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 

Último

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
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
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
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
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
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
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 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
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
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
 

Último (20)

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
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
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
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
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...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
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
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
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 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...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
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
 

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.