SlideShare ist ein Scribd-Unternehmen logo
1 von 23
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

Weitere Àhnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
self_refrential_structures.pptx
self_refrential_structures.pptxself_refrential_structures.pptx
self_refrential_structures.pptx
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
File in C language
File in C languageFile in C language
File in C language
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
String functions in C
String functions in CString functions in C
String functions in C
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Structure in C
Structure in CStructure in C
Structure in C
 
Basic Data Types in C++
Basic Data Types in C++ Basic Data Types in C++
Basic Data Types in C++
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Structure & union
Structure & unionStructure & union
Structure & union
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
C functions
C functionsC functions
C functions
 
File handling in c
File handling in cFile handling in c
File handling in c
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 

Andere mochten auch (7)

Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Strings in C
Strings in CStrings in C
Strings 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
 

Ähnlich wie 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
 
structure and union
structure and unionstructure and union
structure and unionstudent
 
Unit 5 structure and unions
Unit 5 structure and unionsUnit 5 structure and unions
Unit 5 structure and unionskirthika jeyenth
 
Structures
StructuresStructures
Structuresselvapon
 
Structure in c language
Structure in c languageStructure in c language
Structure in c languagesangrampatil81
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing conceptskavitham66441
 
Structure and union
Structure and unionStructure and union
Structure and unionSamsil Arefin
 
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
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptxAnanthi Palanisamy
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3Prerna Sharma
 

Ähnlich wie 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
 
structure and union
structure and unionstructure and union
structure and union
 
Unit 5 structure and unions
Unit 5 structure and unionsUnit 5 structure and unions
Unit 5 structure and unions
 
Structures
StructuresStructures
Structures
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
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
 

Mehr von thirumalaikumar3

Mehr von 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
 

KĂŒrzlich hochgeladen

How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)lakshayb543
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 

KĂŒrzlich hochgeladen (20)

How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)
Visit to a blind student's school🧑‍🩯🧑‍🩯(community medicine)
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPSÂź Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 

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