SlideShare a Scribd company logo
1 of 23
Download to read offline
Lectures on Busy Bee Workshop 1
This session Outline
Structure
Union
Difference between them
Structure & Union -CStructure & Union -C
Lectures on Busy Bee Workshop 2
Data Types
In C programming language, divide the data into
different types. The various data types are
• Simple Data type (Fundamental)
 Integer, Real, Void, Char
• Structured Data type (Derived)
Array, Strings
• User Defined Data type (Programmer)
Enum, Structures, Unions
Structure and UnionStructure and Union
Lectures on Busy Bee Workshop 3
Structure Data Type
1. user defined data type
2. Groups logically related data items of different data
types into a single unit.
3. All the elements of a structure are stored at contiguous
memory locations.
4. A variable of structure type can store multiple data items of
different data types under the one name.
5. As the data of employee in company that is name,
Employee ID, salary, address, phone number is stored in
structure data type.
StructureStructure
Lectures on Busy Bee Workshop 4
StructureStructure
Defining of Structure
A structure has to defined, before it can used. The syntax of
defining a structure is
struct <struct_name>
{
<data_type> <variable_name>;
<data_type> <variable_name>;
……..
<data_type> <variable_name>;
};
Lectures on Busy Bee Workshop 5
StructureStructure
Example of Structure
The structure of Employee is declared as
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};
Memory Space Allocation
8000
8002
8022
8024
8074
8076
employee
emp_id
name[20]
salary
address[50]
dept_no
age
StructureStructure
Declaring a Structure Variable
A structure has to declared, after the body of structure
has defined. The syntax of declaring a structure is
struct <struct_name> <variable_name>;
The example to declare the variable for defined
structure “employee”
struct employee e1;
Here e1 variable contains 6 members that are defined
in structure.
StructureStructure
Initializing a Structure Members
The members of individual structure variable is initialize
one by one or in a single statement. The example to
initialize a structure variable is
1)struct employee e1 = {1, “Suresh”,12000, “3 vikas
colony Madurai”,10, 35};
2)e1.emp_id=1; e1.dept_no=10;
e1.name=“Suresh”; e1.age=35;
e1.salary=12000;
e1.address=“3 vikas colony Madurai”;
StructureStructure
Accessing a Structure Members
1. The structure members cannot be directly accessed in the
expression.
2. They are accessed by using the name of structure variable
followed by a dot and then the name of member
variable.
3. The method used to access the structure variables are
e1.emp_id, e1.name, e1.salary, e1.address, e1.dept_no,
e1.age.
4. The data with in the structure is stored and printed by this
method using scanf and printf statement in c program.
StructureStructure
Structure Assignment
The value of one structure variable is assigned to
another variable of same type using assignment
statement. If the e1 and e2 are structure variables of
type employee then the statement
e1 = e2;
assign value of structure variable e2 to e1. The
value of each member of e2 is assigned to
corresponding members of e1.
StructureStructure
Program to implement the Structure
StructureStructure
StructureStructure
Array of Structure
1. C allows to create an array of variables of
structure.
2. The array of structure is used to store the large
number of similar records.
3. For example to store the record of 100 employees
then array of structure is used.
4. This is similar to array.
5. The syntax to define the array of structure is
struct <struct_name> <var_name(array_name)>
[<value>];
For Example:-
struct employee e1[100];
Program to implement the Array of Structure
StructureStructure
StructureStructure
Structures within Structures
1. C define a variable of structure type as a member of other
structure type.
2. The syntax to define the structure within structure is
struct <struct_name>{
<data_type> <variable_name>;
struct <struct_name>
{ <data_type> <variable_name>;
……..}<struct_variable>;
<data_type> <variable_name>;
};
StructureStructure
Example of Structure within Structure
The structure of Employee is declared as
struct employee
{
int emp_id;
char name[20];
float salary;
int dept_no;
struct date
{
int day;
int month;
int year;
}doj;
}e1;
Accessing Structures within Structures
For Example:-
e1.doj.day;
e1.doj.month;
e1.doj.year;
Pointers and Structures
The pointer variable to structure variable is declared as
struct employee *emp;
Access the Pointer in Structures
Access using arrow operator(->) instead of period operator(.).
For Example:
emp->name; emp->emp_id;
StructureStructure
Passing Structure to Function
For Example:
printdata (struct employee e1); // Function calling
void printdata( struct employee emp) // Function defnition
{
…….
}
Function Returning Structure
struct employee getdata( )
{
….
return e1;
}
StructureStructure
UnionUnion
Union Data Type
1. A union is a user defined data type like structure.
2. The union groups logically related variables into a single
unit.
3. The union data type allocate the space equal to space
need to hold the largest data member of union.
4. The union allows different types of variable to share same
space in memory.
5. There is no other difference between structure and union
than internal difference.
6. The method to declare, use and access the union is same
as structure except the keyword union.
UnionUnion
Example of Union
The union of Employee is declared as
union employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
}e;
Memory Space Allocation
8000
emp_id, dept_no, age
8002
salary
8004
name
8020
address
8050
UnionUnion
Example Program of Union
Structure c
Structure c

More Related Content

What's hot (20)

File handling in c
File handling in cFile handling in c
File handling in c
 
Structure in C
Structure in CStructure in C
Structure in C
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Structure in C
Structure in CStructure in C
Structure in C
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Strings in C
Strings in CStrings in C
Strings in C
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
 
pointer-to-object-.pptx
pointer-to-object-.pptxpointer-to-object-.pptx
pointer-to-object-.pptx
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Pointers
PointersPointers
Pointers
 
Union in c language
Union  in c languageUnion  in c language
Union in c language
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Strings
StringsStrings
Strings
 

Viewers also liked (7)

Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
String functions in C
String functions in CString functions in C
String functions in C
 
Structure in c
Structure in cStructure in c
Structure in c
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Array in c language
Array in c languageArray in c language
Array in c language
 
String c
String cString c
String c
 
String in c
String in cString in c
String in c
 

Similar to Structure c

Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handlingRai University
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Unit 5 structure and unions
Unit 5 structure and unionsUnit 5 structure and unions
Unit 5 structure and unionskirthika jeyenth
 
Structures
StructuresStructures
Structuresselvapon
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing conceptskavitham66441
 
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++Jeff TUYISHIME
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and PointersPrabu U
 
Structure In C
Structure In CStructure In C
Structure In Cyndaravind
 
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.pdfsudhakargeruganti
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3Prerna Sharma
 
Structures in c++
Structures in c++Structures in c++
Structures in c++Swarup Boro
 

Similar to Structure c (20)

Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.3 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.3 pointer, structure ,union and intro to file handling
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Chapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptxChapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptx
 
Unit 5 structure and unions
Unit 5 structure and unionsUnit 5 structure and unions
Unit 5 structure and unions
 
Structures
StructuresStructures
Structures
 
Unit 5 (1)
Unit 5 (1)Unit 5 (1)
Unit 5 (1)
 
Structures and Unions
Structures and UnionsStructures and Unions
Structures and Unions
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Structure and union
Structure and unionStructure and union
Structure and union
 
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++
 
Structures and Pointers
Structures and PointersStructures and Pointers
Structures and Pointers
 
Structure In C
Structure In CStructure In C
Structure 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
 
Structures
StructuresStructures
Structures
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Structures
StructuresStructures
Structures
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 

More from thirumalaikumar3 (8)

Data type in c
Data type in cData type in c
Data type in c
 
Control flow in c
Control flow in cControl flow in c
Control flow in c
 
C function
C functionC function
C function
 
Coper in C
Coper in CCoper in C
Coper in C
 
C basics
C   basicsC   basics
C basics
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
File handling-c programming language
File handling-c programming languageFile handling-c programming language
File handling-c programming language
 
Data type2 c
Data type2 cData type2 c
Data type2 c
 

Recently uploaded

Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxryandux83rd
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfChristalin Nelson
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptxmary850239
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Shark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsShark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsArubSultan
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptx
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdf
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Shark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsShark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristics
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
 

Structure c

  • 1. Lectures on Busy Bee Workshop 1 This session Outline Structure Union Difference between them Structure & Union -CStructure & Union -C
  • 2. Lectures on Busy Bee Workshop 2 Data Types In C programming language, divide the data into different types. The various data types are • Simple Data type (Fundamental)  Integer, Real, Void, Char • Structured Data type (Derived) Array, Strings • User Defined Data type (Programmer) Enum, Structures, Unions Structure and UnionStructure and Union
  • 3. Lectures on Busy Bee Workshop 3 Structure Data Type 1. user defined data type 2. Groups logically related data items of different data types into a single unit. 3. All the elements of a structure are stored at contiguous memory locations. 4. A variable of structure type can store multiple data items of different data types under the one name. 5. As the data of employee in company that is name, Employee ID, salary, address, phone number is stored in structure data type. StructureStructure
  • 4. Lectures on Busy Bee Workshop 4 StructureStructure Defining of Structure A structure has to defined, before it can used. The syntax of defining a structure is struct <struct_name> { <data_type> <variable_name>; <data_type> <variable_name>; …….. <data_type> <variable_name>; };
  • 5. Lectures on Busy Bee Workshop 5 StructureStructure Example of Structure The structure of Employee is declared as struct employee { int emp_id; char name[20]; float salary; char address[50]; int dept_no; int age; };
  • 7. Declaring a Structure Variable A structure has to declared, after the body of structure has defined. The syntax of declaring a structure is struct <struct_name> <variable_name>; The example to declare the variable for defined structure “employee” struct employee e1; Here e1 variable contains 6 members that are defined in structure. StructureStructure
  • 8. Initializing a Structure Members The members of individual structure variable is initialize one by one or in a single statement. The example to initialize a structure variable is 1)struct employee e1 = {1, “Suresh”,12000, “3 vikas colony Madurai”,10, 35}; 2)e1.emp_id=1; e1.dept_no=10; e1.name=“Suresh”; e1.age=35; e1.salary=12000; e1.address=“3 vikas colony Madurai”; StructureStructure
  • 9. Accessing a Structure Members 1. The structure members cannot be directly accessed in the expression. 2. They are accessed by using the name of structure variable followed by a dot and then the name of member variable. 3. The method used to access the structure variables are e1.emp_id, e1.name, e1.salary, e1.address, e1.dept_no, e1.age. 4. The data with in the structure is stored and printed by this method using scanf and printf statement in c program. StructureStructure
  • 10. Structure Assignment The value of one structure variable is assigned to another variable of same type using assignment statement. If the e1 and e2 are structure variables of type employee then the statement e1 = e2; assign value of structure variable e2 to e1. The value of each member of e2 is assigned to corresponding members of e1. StructureStructure
  • 11. Program to implement the Structure StructureStructure
  • 12. StructureStructure Array of Structure 1. C allows to create an array of variables of structure. 2. The array of structure is used to store the large number of similar records. 3. For example to store the record of 100 employees then array of structure is used. 4. This is similar to array. 5. The syntax to define the array of structure is struct <struct_name> <var_name(array_name)> [<value>]; For Example:- struct employee e1[100];
  • 13. Program to implement the Array of Structure StructureStructure
  • 14. StructureStructure Structures within Structures 1. C define a variable of structure type as a member of other structure type. 2. The syntax to define the structure within structure is struct <struct_name>{ <data_type> <variable_name>; struct <struct_name> { <data_type> <variable_name>; ……..}<struct_variable>; <data_type> <variable_name>; };
  • 15. StructureStructure Example of Structure within Structure The structure of Employee is declared as struct employee { int emp_id; char name[20]; float salary; int dept_no; struct date { int day; int month; int year; }doj; }e1;
  • 16. Accessing Structures within Structures For Example:- e1.doj.day; e1.doj.month; e1.doj.year; Pointers and Structures The pointer variable to structure variable is declared as struct employee *emp; Access the Pointer in Structures Access using arrow operator(->) instead of period operator(.). For Example: emp->name; emp->emp_id; StructureStructure
  • 17. Passing Structure to Function For Example: printdata (struct employee e1); // Function calling void printdata( struct employee emp) // Function defnition { ……. } Function Returning Structure struct employee getdata( ) { …. return e1; } StructureStructure
  • 18. UnionUnion Union Data Type 1. A union is a user defined data type like structure. 2. The union groups logically related variables into a single unit. 3. The union data type allocate the space equal to space need to hold the largest data member of union. 4. The union allows different types of variable to share same space in memory. 5. There is no other difference between structure and union than internal difference. 6. The method to declare, use and access the union is same as structure except the keyword union.
  • 19. UnionUnion Example of Union The union of Employee is declared as union employee { int emp_id; char name[20]; float salary; char address[50]; int dept_no; int age; }e;
  • 20. Memory Space Allocation 8000 emp_id, dept_no, age 8002 salary 8004 name 8020 address 8050