Structure and Union
Structure is the collection of elements of different types under a single name for better handling. For example, you want to store the information about person about his/her name, phone number, address, Adhar number and salary etc. You can create this information
separately but, better approach will be collection of these information under single name because all these
information are related to person. Thus, structures are used to store the record in well-organized way. Keyword struct is used for creating a structure.
Syntax of structure:
To create structure variable outside the structure definition, we use following form
Syntax-
struct structurename variablenamel, variablename2, ........variablenameN;
Note: structure variable is created in two ways,
1. structure variable inside in the structure definition
Example:
struct person
{
char name[20];
long int phone;
char address[20];
long int adhar;
float salary;
}p;
2. structure variable outside in the structure definition
Example:
struct
person
{
char name[20];
long int phone;
char address[20];
long int adhar;
float salary;
};
struct
person
p;
Memory Map:
Difference between Arrays and structures -
Initializing structures - A structure can be initialized in much the similar manner as we initialize any other variables.
Example 1 The following program illustrates the assignment of initial values to the members of a structure and displays their values.
Code
#include <stdio.h> void main() { struct student { int rno; char name[25]; float attd; } s = {465, "Leander Paes", 87.50 }; printf("%d %s %.2f", s.rno, s.name, s.attd): }
If we execute this program, then the following output will be generated:
465 Leander Paes 87.50
The following Figure 1 is a schematic representation of the student structure with initial values.
The declaration of the structure in the Program I can also be written, without structure tag, as:
struct
{
int rno;
char name[25];
float attd;
} s = {465, "Leander Paes", 87.50 };
If a structure tag name is not present, more variables of this type cannot be declared later. In contrast, the following declaration has student as a structure tag name.
struct student
{
int rno;
char name[25];
float attd;
};
This structure tag is only a template. Using this template, we can declare any number of variables of
this type.
struct student sl, s2, s3;
In our Program 1, we can write with initial values as: struct student s = {465, "Leander Paes", 87.50 };
Array of structures
The structures that we have defined can only contain one record. In practical applications, there may be a number of records that need to be stored and manipulated. An array of structures is any array that is associated with a structure. The following example would illustrate this concept:
struct
{
int rno;
char name[25];
float attd;
} student[60];
The members of the structure variable (or record) are referred and accessed as follows:
student[i].rno, student[i].name, and student[i].attd, for i = 0 to 59.
Arrays as structure members
We can define structures that contain arrays as members. The following example demonstrates that one character array (string) and another integer array are the members of the structure.
The above code defines an array emp of size 5 elements. Each element of array emp is of type employee. We can also declare an array of structure. Each element of the array representing a structure variable.
Example:
Code
struct employee emp[5]; #include<stdio.h> #include<conio.h> struct employee { char ename[10]; int sal; }; struct employee emp[5]; int i,j; void getinfo() { for(i=0;i<3;i++) { printf("\nEnter %dst employee record\n",i+1); printf("\nEmployee name\t"); scanf("%s",emp[i].ename); printf("\nEnter employee salary\t"); scanf("%d",&emp[i].sal); } } void putinfo() { printf("\nDisplaying Employee record\n"); for(i=0;i<3;i++) { printf("\nEmployee name is %s",emp[i].ename); printf("\nSalary is %d",&mp[i].sal); } } void main() { getinfo(); putinfo(); }
Structure within Structure: Nested Structure
Structure written inside another structure is called as nesting of two structures.
Nested Structures are allowed in C Programming Language.
We can write one Structure inside another structure as member of another structure.
Way 1: Declare two separate structures
struct date
{
int date:
int month:
int year;
};
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date doj;
} empl;
Accessing Nested Elements:
1. Structure members are accessed using dot operator.
2. 'date' structure is nested within Employee Structure.
3. Members of the 'date'can be accessed using 'employee'
4. empl & doj are two structure names (Variables)
Accessing Month Field: empl.doj.month
Accessing day Field: empl.doj.day
Accessing year Field: empl.doj.year
Way 2: Declare Embedded structures
struct Employee
{
char ename[20];
int ssn;
float salary;
struct date
{
int date;
int month;
int year;
}doj;
}empl;
Accessing Nested Members :
Accessing Month Field: empl.doj.month
Accessing day Field: empl.doj.day
Accessing year Field: empl.doj.year
Examples: Nested structure & array with in structure
Code
#include<stdio.h> #include<conio.h> struct date { int dd; int mm; int yy; }; struct student { int rl; struct date dob; int m[3]; int t; }; void main() { struct student list[50]; int i,j,n; printf("enter how many student\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter rollno,date of birth\n"); scanf("%d %d%d %d", &list[i].rl,&list[i].dob.dd,&list[i].dob.mm,&list[i].dob.yy); list[i].t=0; printf("enter three subject marks\n"); for(j=0;j<3;j++) { scanf("%d",&list[i].m[j]); list[i].t+=list[i].m[j]; } } printf("student list\n"); for(i=0;i<n;i++) { printf("%d\t%d-%d-%d\t",list[i].rl, list[i].dob.dd, list[i].dob.mm, list[i].dob.yy); for(j=0;j<3;j++) printf("%d\t",list[i].m[j]); printf("%d\n",list[i].t); } }
Employee Name : Ganesh
Employee SSN : 6700
Employee Salary: 9600.500000
Employee DOJ : 1/11/2015
Structure as function arguments
We can pass a structure as a function argument in similar way as we pass any other variable or array.
Example:
Code
#include<stdio.h> struct student { char name[10]; int roll; }; void show(struct student st); void main() { struct student std; clrscr(); printf("\nEnter student record\n"); printf("\nstudent name\t"); scanf("%s",std.name); printf("\nEnter student roll\t"); scanf("%d", &std.roll); show(std); } void show(struct student st) { printf("\nstudent name is %s",st.name); printf("\nroll is %d",st.roll); }
Pointer to Structure
Like we have array of integers, array of pointer etc, we can also have array of structure variables. And to make the use of array of structure variables efficient, we use pointers of structure type. We can also have pointer to a single structure variable, but it is mostly used with array of structure variables.
Code
struct Book { char name[10]; int price; } int main() { struct Book a; //Single structure variable struct Book* ptr; //Pointer of Structure type ptr = &a; struct Book b[10]; //Array of structure variables struct Book* p; //Pointer of Structure type p=&b; }
Accessing Structure Members with Pointer
To access members of structure with structure variable, we used the dot . operator. But when we have a pointer of structure type, we use arrow -> to access structure members.
Code
struct Book { char name[10]; int price; } void main() { struct Book b; struct Book* ptr = &b; ptr->name = "Dan Brown"; //Accessing Structure Members ptr->price = 500; }
Difference between structure and Union
Post a Comment