SlideShare a Scribd company logo
1 of 19
Download to read offline
Structures and Union
COMPUTER
PROGRAMMING
Topics to be covered
Defining a Structure
Declaring a structure variables
Accessing Structure Elements
Union
Need of Structure
• Problem:
– How to group together a collection of data items
of different types that are logically related to a
particular entity??? (Array)
Solution: Structure
4
Structure
• A structure is a collection of variables of different data
types under a single name.
• The variables are called members of the structure.
• The structure is also called a user-defined data type.
5
Defining a Structure
struct structure_name {
member 1;
member 2;
:
member m;
};
– struct is the required C keyword
– structure_name is the name of the structure
– member 1, member 2, … are individual member
declarations
– The members of a structure do
not occupy memory until they are associated
with a structure_variable.
6
Structure variables
• Once a structure has been defined, the individual
structure-type variables can be declared as:
struct structure_name var_1, var_2, …, var_n;
7
Example
• A structure definition
struct item {
char name[30];
int itemcode;
float price;
};
• Defining structure variables:
struct item a1, a2, a3;
A new data-type
8
Another way of declaring structure
variables
• It is possible to combine the declaration of the
structure with that of the structure variables:
struct item
{
member 1;
member 2;
:
member m;
} var_1, var_2,…, var_n;
• Declares three variables of type struct item
Accessing Structure Elements
• The member variables are accessed using the dot (.)
operator or member operator.
• A structure member can be accessed by writing
variable.member
where variable refers to the name of a structure-type
variable, and member refers to the name of a
member within the structure.
• Examples:
a1.name, a2.name, a1.itemcode, a3.price
Example
#include <stdio.h>
struct complex
{
int real, img;
};
int main()
{
struct complex a, b, c;
printf("Enter a and b where a + ib is the first complex number.n");
scanf("%d%d", &a.real, &a.img);
printf("Enter c and d where c + id is the second complex number.n");
scanf("%d%d", &b.real, &b.img);
c.real = a.real + b.real;
c.img = a.img + b.img;
printf("Sum of the complex numbers: (%d) + (%di)n", c.real, c.img);
return 0;
}
11
Arrays of Structures
• Once a structure has been defined, we can declare
an array of structures
struct item a[5];
– The individual members can be accessed as:
a[i].name
a[2].price;
type name
12
Arrays within Structures
• A structure member can be an array
• The array element within the structure can be
accessed as:
a1.marks[2], a1.dob[3],…
struct student
{
char name[30];
int roll_number;
int marks[5];
char dob[10];
} a1, a2, a3;
13
Structure Initialization
• Structure variables may be initialized following
similar rules of an array. The values are provided
within the second braces separated by commas
• An example:
struct complex a={1.0,2.0}, b={-3.0,4.0};
a.real=1.0; a.imag=2.0;
b.real=-3.0; b.imag=4.0;
Union
• A union is a special data type available in C that
allows to store different data types in the same
memory location.
• You can define a union with many members, but
only one member can contain a value at any given
time.
• A union is declared using the keyword union as:
union student
{
char name[20];
int roll_no;
float marks;
char section;
};
union student s;
• While accessing union members, we should make sure that
we are accessing the member whose value is currently
residing in the memory. Otherwise we will get erroneous
output (which is machine dependent).
Union (Contd…)
Example 1
#include <stdio.h>
#include <string.h>
union Data
{ int i;
float f;
char str[20];
};
int main( )
{
union Data data;
printf( "Memory size occupied by data : %dn", sizeof(data));
return 0;
}
Output:
Memory size occupied by data : 20
Example 2
#include <stdio.h>
#include <string.h>
union Data
{ int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %dn", data.i);
printf( "data.f : %fn", data.f);
printf( "data.str : %sn", data.str);
return 0;
}
Structure vs Union
• Both structure and unions are used to group a
number of different variables together.
Syntactically both structure and unions are exactly
same. The main difference between them is in
storage.
• In structures, each member has its own memory
location but all members of union use the same
memory location which is equal to the greatest
member’s size.
Thank You

More Related Content

Similar to slideset 7 structure and union (1).pdf

Similar to slideset 7 structure and union (1).pdf (20)

1. structure
1. structure1. structure
1. structure
 
Structure in C
Structure in CStructure in C
Structure in C
 
Structures
StructuresStructures
Structures
 
C data types, arrays and structs
C data types, arrays and structsC data types, arrays and structs
C data types, arrays and structs
 
structure1.pdf
structure1.pdfstructure1.pdf
structure1.pdf
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
Structures
StructuresStructures
Structures
 
Data structure and algorithm notes
Data structure and algorithm notesData structure and algorithm notes
Data structure and algorithm notes
 
Unions.pptx
Unions.pptxUnions.pptx
Unions.pptx
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Intake 38 3
Intake 38 3Intake 38 3
Intake 38 3
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)
 
03 structures
03 structures03 structures
03 structures
 
Structures
StructuresStructures
Structures
 
637225564198396290.pdf
637225564198396290.pdf637225564198396290.pdf
637225564198396290.pdf
 

Recently uploaded

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
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
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
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
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
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
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
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 

Recently uploaded (20)

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
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
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...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
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 )
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
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
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 

slideset 7 structure and union (1).pdf

  • 2. Topics to be covered Defining a Structure Declaring a structure variables Accessing Structure Elements Union
  • 3. Need of Structure • Problem: – How to group together a collection of data items of different types that are logically related to a particular entity??? (Array) Solution: Structure
  • 4. 4 Structure • A structure is a collection of variables of different data types under a single name. • The variables are called members of the structure. • The structure is also called a user-defined data type.
  • 5. 5 Defining a Structure struct structure_name { member 1; member 2; : member m; }; – struct is the required C keyword – structure_name is the name of the structure – member 1, member 2, … are individual member declarations – The members of a structure do not occupy memory until they are associated with a structure_variable.
  • 6. 6 Structure variables • Once a structure has been defined, the individual structure-type variables can be declared as: struct structure_name var_1, var_2, …, var_n;
  • 7. 7 Example • A structure definition struct item { char name[30]; int itemcode; float price; }; • Defining structure variables: struct item a1, a2, a3; A new data-type
  • 8. 8 Another way of declaring structure variables • It is possible to combine the declaration of the structure with that of the structure variables: struct item { member 1; member 2; : member m; } var_1, var_2,…, var_n; • Declares three variables of type struct item
  • 9. Accessing Structure Elements • The member variables are accessed using the dot (.) operator or member operator. • A structure member can be accessed by writing variable.member where variable refers to the name of a structure-type variable, and member refers to the name of a member within the structure. • Examples: a1.name, a2.name, a1.itemcode, a3.price
  • 10. Example #include <stdio.h> struct complex { int real, img; }; int main() { struct complex a, b, c; printf("Enter a and b where a + ib is the first complex number.n"); scanf("%d%d", &a.real, &a.img); printf("Enter c and d where c + id is the second complex number.n"); scanf("%d%d", &b.real, &b.img); c.real = a.real + b.real; c.img = a.img + b.img; printf("Sum of the complex numbers: (%d) + (%di)n", c.real, c.img); return 0; }
  • 11. 11 Arrays of Structures • Once a structure has been defined, we can declare an array of structures struct item a[5]; – The individual members can be accessed as: a[i].name a[2].price; type name
  • 12. 12 Arrays within Structures • A structure member can be an array • The array element within the structure can be accessed as: a1.marks[2], a1.dob[3],… struct student { char name[30]; int roll_number; int marks[5]; char dob[10]; } a1, a2, a3;
  • 13. 13 Structure Initialization • Structure variables may be initialized following similar rules of an array. The values are provided within the second braces separated by commas • An example: struct complex a={1.0,2.0}, b={-3.0,4.0}; a.real=1.0; a.imag=2.0; b.real=-3.0; b.imag=4.0;
  • 14. Union • A union is a special data type available in C that allows to store different data types in the same memory location. • You can define a union with many members, but only one member can contain a value at any given time.
  • 15. • A union is declared using the keyword union as: union student { char name[20]; int roll_no; float marks; char section; }; union student s; • While accessing union members, we should make sure that we are accessing the member whose value is currently residing in the memory. Otherwise we will get erroneous output (which is machine dependent). Union (Contd…)
  • 16. Example 1 #include <stdio.h> #include <string.h> union Data { int i; float f; char str[20]; }; int main( ) { union Data data; printf( "Memory size occupied by data : %dn", sizeof(data)); return 0; } Output: Memory size occupied by data : 20
  • 17. Example 2 #include <stdio.h> #include <string.h> union Data { int i; float f; char str[20]; }; int main( ) { union Data data; data.i = 10; data.f = 220.5; strcpy( data.str, "C Programming"); printf( "data.i : %dn", data.i); printf( "data.f : %fn", data.f); printf( "data.str : %sn", data.str); return 0; }
  • 18. Structure vs Union • Both structure and unions are used to group a number of different variables together. Syntactically both structure and unions are exactly same. The main difference between them is in storage. • In structures, each member has its own memory location but all members of union use the same memory location which is equal to the greatest member’s size.