SlideShare a Scribd company logo
UNIT-IV 
STRINGS AND DERIVED TYPES: 
UNIT-IV 
Objective: 
Structures provide a way to organize related data. Unlike arrays, structures allow 
organization of collection of variables. With different data types structures are very useful in 
creating data structures. Unions also provide a way to organize related data, but only one item 
within the union can be used at any time. The structures and unions in C are dealt in this unit. 
INTRODUCTION: 
We have seen arrays can be used to represent a group of data items that belong to the same type , such 
as int or float. However we cannot use an array if we want to represent a collection of data items of different 
types using the single name. C supports the constructed data type known as structures, a mechanism for 
packing data of different types. The concept of a structure is analogous to that of a record in many other 
languages. 
EX: 
Time Seconds(int),Minutes(int),Hours(float) 
Date Day (int),Month(string),Year(int) 
Book Author(string),Title(string),Price(float) 
Address Name(string),Doornumber(string),Street(string),City(string) 
Structures help to organize complex data in a more meaningful way. It is a powerful concept 
that we may often need to use in our program design. 
STRUCTURES DEFINITION: 
M V B REDDY GITAM UNIVERSITY BLR
A structure in C is heterogeneous compound data type, similar to the records of data base and 
PASCAL. It is collection of logically related data items grouped together under a single name called 
structure tag. The data items that make up a structure are known as its members, components, or fields 
and can be of different type. 
THE GENERAL FORMAT: 
struct tag 
{ 
type var1; 
type var2; 
type var3; 
. 
. 
. 
. 
. 
type varn; 
}; 
Ex: struct book_bank 
{ 
char title[25]; 
char author[20]; 
M V B REDDY GITAM UNIVERSITY BLR
int pages; 
float price; 
}; 
Ex: struct book_bank 
{ 
char title[25]; 
char author[20]; 
int pages; 
float price; 
}book1,book2,book3; 
ACCESSING STRUCTURE ELEMENTS: 
The members of structure themselves are not variable.They should be linked to the structure 
variable in order to make them meaningful members .The link between a member and a variable are 
established using a number operator “.” which is also known as dot operator or period operator. For 
example , book2.price is the variable representing the price of book2 and can be treated like any other 
ordinary variables. 
/* DEFINE A STRUCTURE TYPE, STRUCT PERSONAL, THAT WOULD 
CONTAIN PERSON NAME, DATE OF JOINING AND SALARY, USING 
THIS STRUCTURE, WRITE A PROGRAM TO READ THIS INFORMATION 
FOR ONE PERSON FROM THE KEYBOARD AND PRINT THE SAME ON 
THE SCREEN */ 
M V B REDDY GITAM UNIVERSITY BLR
#include<stdio.h> 
struct personal 
{ 
char name[20]; 
int day; 
char month; 
int year; 
float salary; 
main() 
{ 
struct personal person; 
clrscr(); 
printf(“ Enter a person details:nn”); 
printf(“ Enter person name : “); 
scanf(“%s”,person.name); 
printf(“Enter a person joining day : “); 
scanf(“%d”,&person.day); 
printf(“Enter a person joining month: “); 
scanf(“%d”,&person.month); 
printf(“Enter a person joining year: “); 
scanf(“%d”,&person.year); 
M V B REDDY GITAM UNIVERSITY BLR
printf(“Enter a person salary: “); 
scanf(“%d”,&person.salary); 
printf(“nn person’s name is : %sn”,person.name); 
printf(“nn person’s joining day is : %sn”,person.day); 
printf(“nn person’s joining month is : %sn”,person.month); 
printf(“nn person’s joining year is : %sn”,person.year); 
printf(“nn person’s salary is : %sn”,person.salary); 
} 
OUTPUT: 
Enter a person details: 
Enter a person name : Srinivas 
Enter a person joining day: 9 
Enter a person joining month: November 
Enter a person salary: 5260.97 
Enter a person joining year: 1997 
Person’s name is : Srinivas 
Person’s joining day is :9 
Person’s joining month is : November 
Person’s joining year is : 1997 
Person’s salary is :5260.970215 
STRUCTURE INITIALIZATION: 
M V B REDDY GITAM UNIVERSITY BLR
Like other data types a structure variable can be initialized. However a structure must be declared 
as static. 
main() 
{ 
static struct 
{ 
int age; 
float height; 
} 
student={20,180,75}; 
……………… 
……………… 
……………… 
} 
This assigns the value 20 to student.age and 180.75 to student.height. 
Suppose you want to initialize more than one structure variable: 
main() 
{ 
struct st_record 
{ 
M V B REDDY GITAM UNIVERSITY BLR
int age; 
float height; 
}; 
static struct st_record student1={20,180,75}; 
static struct st_record student2={22,177,25}; 
………………….. 
………………….. 
} 
Another method is to initialize a structure variable outside the function 
struct st_record 
{ 
int age; 
float height; 
}student1={20,180,75}; 
main() 
{ 
static struct st_record student2={22,177,25}; 
……….. 
……….. 
COMPARISION OF STRUCTURE VARIABLE: 
Two variables of the same structure type can be compared the same way as ordinary variables. 
M V B REDDY GITAM UNIVERSITY BLR
/*PROGRAM YO ILLUSTRATE THE COMPARISION OF STRUCTURE VARIABLES*/ 
#include<stdio.h> 
struct class 
{ 
int no; 
char name[20]; 
float per; 
}; 
main() 
{ 
int x; 
static struct class stu1={111,”Ramu”,72.50}; 
static struct class stu2={222,”Reddy”,67.00}; 
struct class stu3; 
stu3=stu2; 
if(stu2.no==stu3.no&&stu2.per==stu3.per) 
printf(“n student2 and student3 are samen”); 
else 
printf(“n student2 and student3 are differentn”); 
} 
M V B REDDY GITAM UNIVERSITY BLR
ARRAYS OF STRUCTURES: 
We may declare an array as structures , each element of the array representing a structure 
variable. For example 
struct class student[100]; 
Defines an array called ‘student’ that consists of 100 elements. Each element is defined to be of 
the type struct class. Consider the following declaration 
struct marks 
{ 
int eng; 
int tel; 
int sci; 
}; 
main() 
{ 
static struct marks student[3]={45,76,87},{78,68,79},{34,23,14}; 
……………. 
……………. 
} 
/*WRITE A PROGRAM TO CALCULATE THE SUBJECT-WISE AND STUDENT-WISE TOTALS AND STORE AS A 
PART OF THE STRUCTURE*/ 
#include<stdio.h> 
M V B REDDY GITAM UNIVERSITY BLR
struct marks 
{ 
int tot; 
int eng; 
int tel; 
int sci; 
}; 
main() 
{ 
int i; 
static struct marks student[3]={{45,67,81,0},{75,53,69,0},{57,36,71,0}; 
static struct marks t; 
for(i=0;i<3,i++) 
{ 
student[i].tot=student[i].eng+student[i].tel+student[i].sci; 
t.eng=t.eng+student[i].eng; 
t.tel=t.tel+student[i].tel; 
t.sci=t.sci+student[i].sci; 
t.tot=t.tot+student[i].tot; 
} 
printf(“ STUDENT TOTAL nn”); 
for(i=0;i<3;i++) 
M V B REDDY GITAM UNIVERSITY BLR
{ 
printf(“ stu[%d] : %dn”,i+1,stu[i].tot); 
printf(“ SUBJECT TOTALnn”); 
printf(“English : %dn Telugu : %dn Science : %dn”,t.eng,t.tel,t.sci); 
printf(“n Grand total : %dn”,t.tot); 
} 
ARRAYS WITHIN STRUCTURES: 
C permits the use of array as structure member. We can use single or multi -dimensional array 
of type int or float. 
struct marks 
{ 
int no; 
int sub[5]; 
float fee; 
}stu[10]; 
STRUCTURES WITHIN STRUCTURES: 
Structures within structures means nesting of structures . 
struct employee 
{ 
char name[30]; 
int age; 
struct 
M V B REDDY GITAM UNIVERSITY BLR
{ 
int day; 
char month[20]; 
int year; 
}j_date; 
float sal; 
} 
UNIONS: 
Unions are a concept borrowed from structures and therefore follow the same syntax as 
structures . However there is major distinction between them in terms of storage. In structures each 
member has its own storage location, whereas all the members of a union use the same location. It can 
handle only one member at a time. 
General format: 
union name 
{ 
type var1; 
type var2; 
. 
. 
. 
}; 
Ex: 
union item 
M V B REDDY GITAM UNIVERSITY BLR
{ 
int m; 
float x; 
char c; 
}code; 
This declares a variable code of type union item. The union contains three members each with a 
different data type. 
However we can use only one of them at a time. This is due to the fact that only one location is 
allocated for a union variable, irrespective of its size. 
65497 65498 65499 65500 
c 
m 
M V B REDDY GITAM UNIVERSITY BLR
x 
The compiler allocates a piece of storage that is large enough to hold the largest variable type 
in the union. 
In the declaration above the member x requires 4 bytes which is the largest among the 
members. The above figure shows how all the three variables share the same address. 
ACCESSING UNION ELEMENTS: 
To access a union member we can use the same syntax that we used in the structure members. 
Ex: 
code.m; 
code.x; 
Pointers to remember: 
 Struct is a key word which shows the start of a structure. 
 A structure can be passed to as a single variable to function. 
 A union can also be stored as a member of the structure. 
 Pointers can also store the address of heterogeneous types of elements i.e., structures. 
Key words: 
 Self referential structure. 
 Structure. 
 Union. 
M V B REDDY GITAM UNIVERSITY BLR
 Typedef. 
Sample theory questions: 
1) Distinguish between structure and union/ 
2) When an array of structures is used? Declare a variable as array of structure as initialize 
it? 
3) Write about passing of structures as arguments to functions? 
Sample objective questions: 
1) A bit field is of type integer. 
2) C provides a facility for user defined new data type using typedef. 
3) Structure is a derived data type derived. 
4) Keyword used to represent a structure data type is Structure. 
5) Structure is a data type in which each element that has different Data type. 
6) The member variable of structure are accessed by using dot operator. 
7) Union holds one object at a time. 
M V B REDDY GITAM UNIVERSITY BLR

More Related Content

What's hot

CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
Dhrumil Patel
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
Prerna Sharma
 
Structures
StructuresStructures
When to use a structure vs classes in c++
When to use a structure vs classes in c++When to use a structure vs classes in c++
When to use a structure vs classes in c++
Naman Kumar
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
sangrampatil81
 
Structure in c
Structure in cStructure in c
Structure in c
Prabhu Govind
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
topu93
 
Lecture04 abap on line
Lecture04 abap on lineLecture04 abap on line
Lecture04 abap on lineMilind Patil
 
Structure prespentation
Structure prespentation Structure prespentation
Structure prespentation
ashu awais
 
Structure & union
Structure & unionStructure & union
Structure & union
Rupesh Mishra
 
C data types, arrays and structs
C data types, arrays and structsC data types, arrays and structs
C data types, arrays and structs
Saad Sheikh
 
Datatypes in c
Datatypes in cDatatypes in c
Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9patcha535
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppteShikshak
 
Str
StrStr
Str
Acad
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 

What's hot (20)

CPU : Structures And Unions
CPU : Structures And UnionsCPU : Structures And Unions
CPU : Structures And Unions
 
Programming in C session 3
Programming in C session 3Programming in C session 3
Programming in C session 3
 
Structures
StructuresStructures
Structures
 
When to use a structure vs classes in c++
When to use a structure vs classes in c++When to use a structure vs classes in c++
When to use a structure vs classes in c++
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Unit4 C
Unit4 C Unit4 C
Unit4 C
 
Structure in c
Structure in cStructure in c
Structure in c
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
 
Lecture04 abap on line
Lecture04 abap on lineLecture04 abap on line
Lecture04 abap on line
 
Structure prespentation
Structure prespentation Structure prespentation
Structure prespentation
 
Structure & union
Structure & unionStructure & union
Structure & union
 
Structure in c
Structure in cStructure in c
Structure in c
 
C data types, arrays and structs
C data types, arrays and structsC data types, arrays and structs
C data types, arrays and structs
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
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
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
 
Str
StrStr
Str
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 

Similar to C UNIT-4 PREPARED BY M V BRAHMANANDA RE

Structure & union
Structure & unionStructure & union
Structure & union
lalithambiga kamaraj
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
C structure and union
C structure and unionC structure and union
C structure and union
Thesis Scientist Private Limited
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
Mehul Desai
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
kavitham66441
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
ansariparveen06
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
ParinayWadhwa
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)mrecedu
 
Structures
StructuresStructures
Structures
Mitali Chugh
 
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 in c++
Structures in c++Structures in c++
Structures in c++
Swarup Boro
 
Structure In C
Structure In CStructure In C
Structure In C
yndaravind
 
Structures
StructuresStructures
Structures
DrJasmineBeulahG
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
Dhrumil Patel
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
Tanmay Modi
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
tanmaymodi4
 
Unit 1_ADC.pptx
Unit 1_ADC.pptxUnit 1_ADC.pptx
Unit 1_ADC.pptx
Itsbrokenstatus
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
nikshaikh786
 

Similar to C UNIT-4 PREPARED BY M V BRAHMANANDA RE (20)

Structure & union
Structure & unionStructure & union
Structure & union
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdfSTRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
 
C structure and union
C structure and unionC structure and union
C structure and union
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
 
structures_v1.ppt
structures_v1.pptstructures_v1.ppt
structures_v1.ppt
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
Structures
StructuresStructures
Structures
 
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 in c++
Structures in c++Structures in c++
Structures in c++
 
Structure In C
Structure In CStructure In C
Structure In C
 
Structures
StructuresStructures
Structures
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
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
 
Unit 1_ADC.pptx
Unit 1_ADC.pptxUnit 1_ADC.pptx
Unit 1_ADC.pptx
 
Module 5-Structure and Union
Module 5-Structure and UnionModule 5-Structure and Union
Module 5-Structure and Union
 

C UNIT-4 PREPARED BY M V BRAHMANANDA RE

  • 1. UNIT-IV STRINGS AND DERIVED TYPES: UNIT-IV Objective: Structures provide a way to organize related data. Unlike arrays, structures allow organization of collection of variables. With different data types structures are very useful in creating data structures. Unions also provide a way to organize related data, but only one item within the union can be used at any time. The structures and unions in C are dealt in this unit. INTRODUCTION: We have seen arrays can be used to represent a group of data items that belong to the same type , such as int or float. However we cannot use an array if we want to represent a collection of data items of different types using the single name. C supports the constructed data type known as structures, a mechanism for packing data of different types. The concept of a structure is analogous to that of a record in many other languages. EX: Time Seconds(int),Minutes(int),Hours(float) Date Day (int),Month(string),Year(int) Book Author(string),Title(string),Price(float) Address Name(string),Doornumber(string),Street(string),City(string) Structures help to organize complex data in a more meaningful way. It is a powerful concept that we may often need to use in our program design. STRUCTURES DEFINITION: M V B REDDY GITAM UNIVERSITY BLR
  • 2. A structure in C is heterogeneous compound data type, similar to the records of data base and PASCAL. It is collection of logically related data items grouped together under a single name called structure tag. The data items that make up a structure are known as its members, components, or fields and can be of different type. THE GENERAL FORMAT: struct tag { type var1; type var2; type var3; . . . . . type varn; }; Ex: struct book_bank { char title[25]; char author[20]; M V B REDDY GITAM UNIVERSITY BLR
  • 3. int pages; float price; }; Ex: struct book_bank { char title[25]; char author[20]; int pages; float price; }book1,book2,book3; ACCESSING STRUCTURE ELEMENTS: The members of structure themselves are not variable.They should be linked to the structure variable in order to make them meaningful members .The link between a member and a variable are established using a number operator “.” which is also known as dot operator or period operator. For example , book2.price is the variable representing the price of book2 and can be treated like any other ordinary variables. /* DEFINE A STRUCTURE TYPE, STRUCT PERSONAL, THAT WOULD CONTAIN PERSON NAME, DATE OF JOINING AND SALARY, USING THIS STRUCTURE, WRITE A PROGRAM TO READ THIS INFORMATION FOR ONE PERSON FROM THE KEYBOARD AND PRINT THE SAME ON THE SCREEN */ M V B REDDY GITAM UNIVERSITY BLR
  • 4. #include<stdio.h> struct personal { char name[20]; int day; char month; int year; float salary; main() { struct personal person; clrscr(); printf(“ Enter a person details:nn”); printf(“ Enter person name : “); scanf(“%s”,person.name); printf(“Enter a person joining day : “); scanf(“%d”,&person.day); printf(“Enter a person joining month: “); scanf(“%d”,&person.month); printf(“Enter a person joining year: “); scanf(“%d”,&person.year); M V B REDDY GITAM UNIVERSITY BLR
  • 5. printf(“Enter a person salary: “); scanf(“%d”,&person.salary); printf(“nn person’s name is : %sn”,person.name); printf(“nn person’s joining day is : %sn”,person.day); printf(“nn person’s joining month is : %sn”,person.month); printf(“nn person’s joining year is : %sn”,person.year); printf(“nn person’s salary is : %sn”,person.salary); } OUTPUT: Enter a person details: Enter a person name : Srinivas Enter a person joining day: 9 Enter a person joining month: November Enter a person salary: 5260.97 Enter a person joining year: 1997 Person’s name is : Srinivas Person’s joining day is :9 Person’s joining month is : November Person’s joining year is : 1997 Person’s salary is :5260.970215 STRUCTURE INITIALIZATION: M V B REDDY GITAM UNIVERSITY BLR
  • 6. Like other data types a structure variable can be initialized. However a structure must be declared as static. main() { static struct { int age; float height; } student={20,180,75}; ……………… ……………… ……………… } This assigns the value 20 to student.age and 180.75 to student.height. Suppose you want to initialize more than one structure variable: main() { struct st_record { M V B REDDY GITAM UNIVERSITY BLR
  • 7. int age; float height; }; static struct st_record student1={20,180,75}; static struct st_record student2={22,177,25}; ………………….. ………………….. } Another method is to initialize a structure variable outside the function struct st_record { int age; float height; }student1={20,180,75}; main() { static struct st_record student2={22,177,25}; ……….. ……….. COMPARISION OF STRUCTURE VARIABLE: Two variables of the same structure type can be compared the same way as ordinary variables. M V B REDDY GITAM UNIVERSITY BLR
  • 8. /*PROGRAM YO ILLUSTRATE THE COMPARISION OF STRUCTURE VARIABLES*/ #include<stdio.h> struct class { int no; char name[20]; float per; }; main() { int x; static struct class stu1={111,”Ramu”,72.50}; static struct class stu2={222,”Reddy”,67.00}; struct class stu3; stu3=stu2; if(stu2.no==stu3.no&&stu2.per==stu3.per) printf(“n student2 and student3 are samen”); else printf(“n student2 and student3 are differentn”); } M V B REDDY GITAM UNIVERSITY BLR
  • 9. ARRAYS OF STRUCTURES: We may declare an array as structures , each element of the array representing a structure variable. For example struct class student[100]; Defines an array called ‘student’ that consists of 100 elements. Each element is defined to be of the type struct class. Consider the following declaration struct marks { int eng; int tel; int sci; }; main() { static struct marks student[3]={45,76,87},{78,68,79},{34,23,14}; ……………. ……………. } /*WRITE A PROGRAM TO CALCULATE THE SUBJECT-WISE AND STUDENT-WISE TOTALS AND STORE AS A PART OF THE STRUCTURE*/ #include<stdio.h> M V B REDDY GITAM UNIVERSITY BLR
  • 10. struct marks { int tot; int eng; int tel; int sci; }; main() { int i; static struct marks student[3]={{45,67,81,0},{75,53,69,0},{57,36,71,0}; static struct marks t; for(i=0;i<3,i++) { student[i].tot=student[i].eng+student[i].tel+student[i].sci; t.eng=t.eng+student[i].eng; t.tel=t.tel+student[i].tel; t.sci=t.sci+student[i].sci; t.tot=t.tot+student[i].tot; } printf(“ STUDENT TOTAL nn”); for(i=0;i<3;i++) M V B REDDY GITAM UNIVERSITY BLR
  • 11. { printf(“ stu[%d] : %dn”,i+1,stu[i].tot); printf(“ SUBJECT TOTALnn”); printf(“English : %dn Telugu : %dn Science : %dn”,t.eng,t.tel,t.sci); printf(“n Grand total : %dn”,t.tot); } ARRAYS WITHIN STRUCTURES: C permits the use of array as structure member. We can use single or multi -dimensional array of type int or float. struct marks { int no; int sub[5]; float fee; }stu[10]; STRUCTURES WITHIN STRUCTURES: Structures within structures means nesting of structures . struct employee { char name[30]; int age; struct M V B REDDY GITAM UNIVERSITY BLR
  • 12. { int day; char month[20]; int year; }j_date; float sal; } UNIONS: Unions are a concept borrowed from structures and therefore follow the same syntax as structures . However there is major distinction between them in terms of storage. In structures each member has its own storage location, whereas all the members of a union use the same location. It can handle only one member at a time. General format: union name { type var1; type var2; . . . }; Ex: union item M V B REDDY GITAM UNIVERSITY BLR
  • 13. { int m; float x; char c; }code; This declares a variable code of type union item. The union contains three members each with a different data type. However we can use only one of them at a time. This is due to the fact that only one location is allocated for a union variable, irrespective of its size. 65497 65498 65499 65500 c m M V B REDDY GITAM UNIVERSITY BLR
  • 14. x The compiler allocates a piece of storage that is large enough to hold the largest variable type in the union. In the declaration above the member x requires 4 bytes which is the largest among the members. The above figure shows how all the three variables share the same address. ACCESSING UNION ELEMENTS: To access a union member we can use the same syntax that we used in the structure members. Ex: code.m; code.x; Pointers to remember:  Struct is a key word which shows the start of a structure.  A structure can be passed to as a single variable to function.  A union can also be stored as a member of the structure.  Pointers can also store the address of heterogeneous types of elements i.e., structures. Key words:  Self referential structure.  Structure.  Union. M V B REDDY GITAM UNIVERSITY BLR
  • 15.  Typedef. Sample theory questions: 1) Distinguish between structure and union/ 2) When an array of structures is used? Declare a variable as array of structure as initialize it? 3) Write about passing of structures as arguments to functions? Sample objective questions: 1) A bit field is of type integer. 2) C provides a facility for user defined new data type using typedef. 3) Structure is a derived data type derived. 4) Keyword used to represent a structure data type is Structure. 5) Structure is a data type in which each element that has different Data type. 6) The member variable of structure are accessed by using dot operator. 7) Union holds one object at a time. M V B REDDY GITAM UNIVERSITY BLR