ALL SIR NOTES HAVE BEEN UPLOADED HERE.
C supports a constructed data type known as structure, a mechanism for packing data of different types. Structure is a user-defined data type in C language which allows us to combine data of different types together. A structure is a convenient tool for handling a group of logically related data items. Structure help to organize complex data in a more meaningful way.
Syntax:
struct <structure_name>
{
DataType member_1;
DataType member_2;
......
DataType member_n;
};
Unlike arrays, structures must be defined first for their format that may be used later to
declare structure variables. For example:
struct record
{
int rollno;
char name[20];
float per;
};
struct record student1, student2;
The keyword struct declares a structure to hold the details of three data fields, namely
rollno, name and per. These fields are called structure elements or members. Each member may belong to a different type of data. record is the name of the structure and is called the structure tag.
Student1 and student2 are the variable of type struct record. There are two ways to declare structure variable:
By struct keyword within main() function
By declaring a variable at the time of defining the structure.
The link between a member and a variable is established using the member operator ‘.’ Which is also known as ‘dot operator’. For example:
student1.roll = 201;
strcpy(student1.name, “Bhagirath”);
We can also use scanf function to give the values through the keyboard. To access pointer
variable, we use the -> (arrow operator).
Like any other data type, a structure variable can be initialized at compile time like:
struct record
{
int rollno;
char name[20];
float per;
};
struct record student1 = { 201, “Bhagirath”, 88.0 };
We use structures to describe the format of a number of related variables. We may declare an array of structures, each element of the array representing a structure variable.
struct record student[50];
student[0].rollno = 101;
strcpy ( student[0].name, “Bhagirath”);
defines an array called student, that consists of 50 elements. Each element is defined to be of the type struct record and assign values to the members.
Example:
#include <stdio.h>
struct student
{
int rollno;
char name[20];
};
int main()
{
struct student stu[5];
int i;
for( i = 0; i < 5; i++)
{
printf(“\nEnter roll Number: “);
scanf(“%d”,&stu[i].rollno);
printf(“\nEnter name: “);
gets(stu[i].name);
}
for( i = 0; i < 5; i++)
{
printf(“\nRoll Number: %d”, stu[i].rollno);
printf(“\nName: %s“, stu[i].name);
}
return 0;
}
Structure within a structure means nesting of structures. Nesting of structures is permitted in C. For example:
struct record{int rollno;char name;struct{int phy;int che;int math;} marks;} student;The record structure contains a member named marks, which itself is a structure with three members.
An inner-most member in a nested structure can be accessed by chaining all the concerned structure variable with the member using dot operator. For example:
student.marks.phy = 83;
Example:
#include <stdio.h>struct student{int rollno;char name[30];struct marks{int eng, math;float per;} score;};int main(){struct student obj = {101, "Bhagirath", 87, 76};obj.score.per = (obj.score.eng + obj.score.math) / 2;printf("\nRecord is: \n");printf("\nRoll Number: %d", obj.rollno);printf("\nName : %s", obj.name);printf("\nMarks in eng: %d", obj.score.eng);printf("\nMarks in math: %d", obj.score.math);printf("\nPercentage: %.2f", obj.score.per);return 0;}There are three methods by which the values of a structure can be transferred from one function to another:
The first method is to pass each member of the structure as an actual argument.
The second method involves passing of a copy of the entire structure.
The third method employs passing address of a structure to a function.
The general format of sending a copy of a structure to the called function is:
Function_name ( structure_variable );
When a structure is used as an argument to a function, the entire structure is passed using the call by value method. When using a structure as a parameter, remember that the type of the argument must match the type of the parameter.
structure which contain a member field that point to the same structure type are called self referential structures. Each structure consist of two fields, one containing the item, and the other containing the address of the next item (a pointer to the next item).
For example:
struct node
{
int code;
struct node *next;
};
Such a structure is also called dynamic data structure. Using self-referential structure, a list can grow or shrink in size during the execution of a program. This list does not waste memory space. It uses the memory that is just needed for the list at any point of time.
Structure padding is a concept in C that adds the one or more empty bytes between the memory addresses to align the data in memory. For example:
struct student{char ch1; //1 bytechar ch2; //1 byteint num; //4 byte} obj;int main(){printf(“\nThe size of obj = %d bytes”, sizeof(obj) );return 0;}Output:The size of obj = 8 bytes //due to concept of structure padding.Example:
#include <stdio.h>#pragma pack(1)struct number{int num;char ch;double d;};int main(){struct number obj;printf("\nSize of obj = %d bytes",sizeof(obj));return 0;}Output:Size of obj = 13 bytesLike structures, union is a user defined data type. In union, all members share the same memory location. This implies that, although a union may contain many members of different types, it can handle only one member at a time. Like structures, a union can be declared using the keyword union as follows:
union item
{
char c;
int m;
float x;
} 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. The compiler allocates a piece of storage that is large enough to hold the largest variable type in the union.
The enumerated data type gives us an opportunity to invent our own data type and define what values the variable of this data type can take. This can help in making the program listings more readable.
enum dept{mca = 21, mba = 11, btech = 10, bca = 30};enum dept s1,s2,s3;Now we can give values to these variables.
s1 = mca;s2 = bca;Internally, the compiler treats the enumerator as integers. Each value on the list of
permissible values corresponds to an integer, starting with 0. This way of assigning
numbers can be overridden by the programmer by initializing the enumerators to different
integer values.