The document discusses structures and unions in C. Structures allow organizing related data of different types under a single name, while unions share the same memory location for members. The document covers defining and initializing structures, accessing structure members, arrays of structures, nested structures, and unions. It provides examples of declaring and using structures and unions, as well as comparing and passing structures to functions.
C Programming Language is the most popular computer language and most used programming language till now. It is very simple and elegant language. This lecture series will give you basic concepts of structured programming language with C.
C Programming Language is the most popular computer language and most used programming language till now. It is very simple and elegant language. This lecture series will give you basic concepts of structured programming language with C.
A structure is a collection of variables referenced under one name providing a convenient means of keeping related information together. The structure definition creates a format that may be used to declare structure variables in a program later. data structure in c questions and answer
Structures in C
What is a structure?
A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.
● Concept of Structure and Union
● Declaration and Initialization of structure and union
● Nested structures
● Array of Structures
● Passing structure to functions
Similar to C UNIT-4 PREPARED BY M V BRAHMANANDA RE (20)
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