SlideShare a Scribd company logo
1 of 14
PROGRAMMING IN 
C
CONTENTS 
STRUCTURE 
UNION
STRUCTURE 
Structure is user define data type. When we defined a 
structure as a group of elements of different data-type 
held together in a single unit .The individual 
elements are called data members of Structure. 
Members of the structure can be accessed & 
processed separately. The graphical representation 
of a structure is : 
Structure name 
Member-1 
Member-2 
Member-3 
Member-n
Syntax : 
struct name 
{ 
data-type member-1; 
data-type member-2; 
data-type member-3; 
----------------------------- 
----------------------------- 
data-type member-n; 
}
ACCESSING MEMBERS OF STRUCTURE 
The members of a structure variable are accessed using dot 
operator(.). The syntax is 
structure-variable. member name 
Example: struct date 
{ 
int dd; 
int mm; 
int yy; 
} 
dob; 
The values to members of structure variable dob are assigned 
as: 
dob.dd=05; 
dob.mm=07; 
dob.yy=2014;
INITIALIZATION OF STRUCTURES 
Like variables of other data types, a structure 
variable can be initialized. For illustration consider 
the following segment. 
struct std 
{ 
int rollno; 
char name[25]; 
char Dept[25]; 
}; 
struct std student={44,”Divesh”,”IT”};
FUNCTIONS & STRUCTURES 
In C ,structure can be passed to a function as 
a single variable. The scope of a structure 
declaration should be an external storage 
class whenever a function in the main 
program is using a structure data type. The 
member data should be same throughout the 
program. Individual structure members can 
be passed to a function as arguments in the 
function call & a single structure member can 
be returned through the return statement.
POINTER & STRUCTURE 
Through the use of (&) address operator, starting 
address of a structure can be accessed in the 
same manner as any other address. Therefore, if 
variable represents a structure type variable the 
& variable represents the beginning address of 
that variable. A pointer variable for a structure 
declare as : 
type *ptr 
to assign the starting address of a structure 
variable to pointer as 
ptr=&variable
Example: 
struct 
{ 
int acc_no; 
char acc_type; 
char customer[50]; 
float bal_amount; 
} 
s_customer,*ptr_cust; 
Pointer structure can be accessed & processed in 
following way: 
(*structure_name).field name_variable;
ARRAY OF STRUCTURES 
When we want to process a list of 
values of structure type, it is 
required to use an array of 
structures. The array of structures 
are defined as simple structures, 
only the difference is that instead 
of a simple variable, we specify 
array variable name for structure.
Example: 
struct stud 
{ 
int rollno; 
char name[25]; 
char dept[25]; 
int height; 
}; 
struct stud student[100];
NESTED STRUCTURES 
A structure can be embedded in another structure. The concept 
of nested structure illustrated by following example: 
struct name 
{ 
char fname[20]; 
char mname[20]; 
}; 
struct sal 
{ 
char empno[5]; 
char dept[15]; 
float salary; 
} 
employee;
UNION 
Union is quite similar to structure but processing & storage 
is little different. Both contains a group of members 
which may be off different data types but in structure 
each member has its own memory location, whereas in 
union all the members share the same storage area. 
Syntax- Union tag-name 
{ 
data-type member1; 
data-type member 2; 
----------------------------- 
data-type member n; 
};
DIFFERENCE 
Structure Union 
Structure allocates storage space for 
all its members separately. 
Union allocates one common storage 
space for all its members. 
Union finds that which of its member 
needs high storage space over other 
members and allocates that much space 
Structure occupies higher memory 
space. 
Union occupies lower memory space 
over structure. 
We can access all members of structure 
at a time. 
We can access only one member of 
union at a time. 
All members may be initialized. Only first member may be initialized. 
Consume more space than union. Conservation of memory is possible. 
Syntax: struct name 
{ 
data-type member-1; 
data-type member-2; 
---------------------- 
data-type member-n; 
} 
Syntax: Union tag-name 
{ 
data-type member1; 
data-type member 2; 
------------------------- 
data-type member n; 
}

More Related Content

What's hot (20)

Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
One Dimensional Array
One Dimensional Array One Dimensional Array
One Dimensional Array
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
C pointer
C pointerC pointer
C pointer
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
Array in C
Array in CArray in C
Array in C
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
pointers
pointerspointers
pointers
 
Introduction to microprocessor
Introduction to microprocessorIntroduction to microprocessor
Introduction to microprocessor
 
NumPy
NumPyNumPy
NumPy
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 

Similar to Programming in C session 3

Similar to Programming in C session 3 (20)

Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
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
 
Str
StrStr
Str
 
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
Structure In C
Structure In CStructure In C
Structure In C
 
CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
#Jai c presentation
#Jai c presentation#Jai c presentation
#Jai c presentation
 
User defined data types.pptx
User defined data types.pptxUser defined data types.pptx
User defined data types.pptx
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and Typedef
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Structures
StructuresStructures
Structures
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
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
 
UNIONS.pptx
UNIONS.pptxUNIONS.pptx
UNIONS.pptx
 
Chapter 7 (Part I) - User Defined Datatypes.pdf
Chapter 7 (Part I) - User Defined Datatypes.pdfChapter 7 (Part I) - User Defined Datatypes.pdf
Chapter 7 (Part I) - User Defined Datatypes.pdf
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
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
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
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
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
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
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
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
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

Programming in C session 3

  • 3. STRUCTURE Structure is user define data type. When we defined a structure as a group of elements of different data-type held together in a single unit .The individual elements are called data members of Structure. Members of the structure can be accessed & processed separately. The graphical representation of a structure is : Structure name Member-1 Member-2 Member-3 Member-n
  • 4. Syntax : struct name { data-type member-1; data-type member-2; data-type member-3; ----------------------------- ----------------------------- data-type member-n; }
  • 5. ACCESSING MEMBERS OF STRUCTURE The members of a structure variable are accessed using dot operator(.). The syntax is structure-variable. member name Example: struct date { int dd; int mm; int yy; } dob; The values to members of structure variable dob are assigned as: dob.dd=05; dob.mm=07; dob.yy=2014;
  • 6. INITIALIZATION OF STRUCTURES Like variables of other data types, a structure variable can be initialized. For illustration consider the following segment. struct std { int rollno; char name[25]; char Dept[25]; }; struct std student={44,”Divesh”,”IT”};
  • 7. FUNCTIONS & STRUCTURES In C ,structure can be passed to a function as a single variable. The scope of a structure declaration should be an external storage class whenever a function in the main program is using a structure data type. The member data should be same throughout the program. Individual structure members can be passed to a function as arguments in the function call & a single structure member can be returned through the return statement.
  • 8. POINTER & STRUCTURE Through the use of (&) address operator, starting address of a structure can be accessed in the same manner as any other address. Therefore, if variable represents a structure type variable the & variable represents the beginning address of that variable. A pointer variable for a structure declare as : type *ptr to assign the starting address of a structure variable to pointer as ptr=&variable
  • 9. Example: struct { int acc_no; char acc_type; char customer[50]; float bal_amount; } s_customer,*ptr_cust; Pointer structure can be accessed & processed in following way: (*structure_name).field name_variable;
  • 10. ARRAY OF STRUCTURES When we want to process a list of values of structure type, it is required to use an array of structures. The array of structures are defined as simple structures, only the difference is that instead of a simple variable, we specify array variable name for structure.
  • 11. Example: struct stud { int rollno; char name[25]; char dept[25]; int height; }; struct stud student[100];
  • 12. NESTED STRUCTURES A structure can be embedded in another structure. The concept of nested structure illustrated by following example: struct name { char fname[20]; char mname[20]; }; struct sal { char empno[5]; char dept[15]; float salary; } employee;
  • 13. UNION Union is quite similar to structure but processing & storage is little different. Both contains a group of members which may be off different data types but in structure each member has its own memory location, whereas in union all the members share the same storage area. Syntax- Union tag-name { data-type member1; data-type member 2; ----------------------------- data-type member n; };
  • 14. DIFFERENCE Structure Union Structure allocates storage space for all its members separately. Union allocates one common storage space for all its members. Union finds that which of its member needs high storage space over other members and allocates that much space Structure occupies higher memory space. Union occupies lower memory space over structure. We can access all members of structure at a time. We can access only one member of union at a time. All members may be initialized. Only first member may be initialized. Consume more space than union. Conservation of memory is possible. Syntax: struct name { data-type member-1; data-type member-2; ---------------------- data-type member-n; } Syntax: Union tag-name { data-type member1; data-type member 2; ------------------------- data-type member n; }

Editor's Notes

  1. By prerna sharma