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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
structure and union
structure and unionstructure and union
structure and union
 
OOP Assignment 03.pdf
OOP Assignment 03.pdfOOP Assignment 03.pdf
OOP Assignment 03.pdf
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Types of operators in C
Types of operators in CTypes of operators in C
Types of operators in C
 
Structure & union
Structure & unionStructure & union
Structure & union
 
pointers
pointerspointers
pointers
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Structure in C
Structure in CStructure in C
Structure in C
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 

Ähnlich wie Programming in C session 3

Ähnlich wie 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
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
#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 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
 
Structure prespentation
Structure prespentation Structure prespentation
Structure prespentation
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
 

Kürzlich hochgeladen

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 

Kürzlich hochgeladen (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

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; }

Hinweis der Redaktion

  1. By prerna sharma