Structure: A Structure Is A Group of Data Items of Different Data Types Held Together in A Single Unit
Structure: A Structure Is A Group of Data Items of Different Data Types Held Together in A Single Unit
type var2;
type var3;
. Structure template
struct employee_type
{
int code;
char name[20];
int dept_code;
float salary;
};
No variable has been associated with this structure
No memory is set aside for this structure.
Declaring Variables of the Structure Type
Declares a variable employee of type employee_type
struct employee_type employee;
At this point, the memory is set aside for the structure variable
employee.
We can also declare variable(s) of structure type along
with the structure declaration. struct employee_type
{
int code;
char name[20];
int dept_code;
float salary;
}employee
Consider the declarations to understand how the elements
of the structure variables are stored in memory
struct example_type
{
char var1;
int var2;
float var3;
double var4;
};
struct example_type sample1;
Note: all members are stored in contiguous memory location in order in which they are
declared.
How the elements of the structure variables are
stored in memory
1197
1198
var 1 1199
1200
1201
var 2 1202
1203
1204
1205
var 3
1206
1207
1208
var 4 1209
1210
1211
Intializing Structures
struct student_type
{
int rollno;
char name[25];
int age;
float height;
};
struct student_type student={1000,Surbhi salaria,18,5.6};
Accessing Structure Elements
Elements are accessed using dot operator.
It provides a powerful and clear way to refer to an
individual element.
Syntax: sname.vname
sname is structure variable name.
vname is name of the element of the structure.
Eg: the element of the structure variable student
can be accessed as
student.rollno,student.name,student.age,student.height
WAP to display size of structure elements.Use sizeof() of operator.
main() Output
{ Book : 30
struct book1 Pages:2
{ Price:4
char book[30]; Total Bytes:36
int pages;
float price;
};
struct book1 bk1;
clrscr();
printf(\nSize of Structure elements\n);
printf(\nBook: %d,sizeof(bk1.book));
printf(\nPages: %d,sizeof(bk1.pages));
printf(\nBook:%d,sizeof(bk1.price));
printf(\nBook:%d,sizeof(bk1));
}
Entering Data into Structures
struct employee_type
{
int code;
char name[25];
char dept[15];
float salary;
};
main()
{ continue
struct employee_type employee;
printf(\n\nParticulars of emp as entered by user\n);
printf(\nEnter employee code:\n);
scanf(%d,&employee.code); printf(\nEmployees code:%d,employee.code);
printf(\nEnter name:\n);
gets(employee.name); printf(\nEmployees name:%s, employee.name);
printf(\nEnter employees dept:\n);
gets(employee.dept); printf(\nEmployees dept:%s,employee.dept);
printf(\nEnter employees salary:\n);
scanf(%f,&employee.salary); Printf(\nEmployees sal:%f,employee.salary);
continue
Use of Assignment Statement for Structures
Value of one structure variable can be assigned to
another variable of the same type using simple
assignment statement.if student1 and student2
are structure variable of type student_type,then
student2=student1;
Assigns value of structure variable student1 to
student2
employee[6].salary
WAP to create an array of structure objects
main() printf(\n Car no Starting time Reaching time);
for(k=0;k<3;k++)
{
{
int k;
scanf(%d,&r[k].carno);
struct time scanf(%d %d %d, &r1[k].st.hour,&r1[k].st.minute,&r1[k].st.second);
{ scanf(%d %d %d, &r1[k].rt.hour,&r1[k].rt.minute,&r1[k].rt.second);
int second; }
int minute; for(k=0;k<3;k++)
{
int hour;
printf(%d,r1[k].carno);
}; printf(%d %d %d,r1[k].st.hour,r1[k].st.minute,r1[k].st.second);
struct t printf(%d %d %d,r1[k].rt.hour,r1[k].rt.minute,r1[k].rt.second);
{ }
int carno; }
struct time st;
struct time rt;
};
struct t r1[3];
clrscr();
Structure and Function
The relationship of structure with the function
can be viewed from three angles:-
1. Passing Structures to a function.
2. Function Returning Structure.
3. Passing array of Structures to Function.
Note:
When a structure element is to be passed to any other
function,it is essential to declare the structure outside the
main() functioni.e global
Passing Structure to a Function
Similar to passing array of variable,structure can be
passed to a function as argument
Syntax:
type-specifier func-name(struct-variable); /*actual
argument*/
Read and display student grade by
using structure with function
struct student Void disp(student s)
{ {
int rn; printf(\nRollno is %d,s.rn);
char name[20]; printf(\n Name is %s,s.name);
char grade[10]; printf(\n Grade is: %s,s.grade);
}; grade;
Struct student s; }
Void disp(student);
main()
{
printf(\nEnter rollno,name and grade of student:\n);
scanf(%d %s %s,&s.rn,&s.name,&s.grade);
disp(s);
getch();
}
Passing of structure variables by value to a function
# include<stdio.h>
# include<conio.h>
disp(s);
getch();
}
Passing to the function by address
struct book
{
char name[35];
char author[35];
int pages;
}b1;
void main() show(struct book *b2)
{
{ ------------------
--------- -------------------
}
---------
show(&b1);
}
Prog to pass address of structure variable to user defined function and
display the contents
#include<stdio.h>
#include<conio.h>
struct book
{
char name[35];
char author[35];
int pages;
};
void show(book*);
main()
{
struct book b1={"JAVA COMPETE REFERENCE","P.NAUGHTON",886};
show(&b1);
getch();
}
void show(struct book *b2) // pointer to structure
{
printf("%s %s %d",b2->name,b2->author,b2->pages);
}
Passing of structure variables by reference to a function
struct date
{
int day;
int month;
int year;
};
main()
{
struct date d;
void get_date(struct date *);
printf(Enter date in the format:\n);
get_date(&d);
printf(\nDate entered by you %d %d %d,d.day,d.month,d.year);
}
void get_date(struct date *a)
{ scanf(%d %d %d,&a->day,&a->month,a->year);
}
Wap to pass structure elements to function print() and print the
elements
void main()
Note:
{
We have passed base address of name
struct boy But values of age and wt.
{ Here values are passed using call by reference and call by
char name[25]; value
int age;
int wt;
};
struct boy b1={Amit,20,25};
print(b1.name,b1.age,b1.wt);
}
print(char *s,int t, int n)
{
printf(\n%s %d %d,s,t,n);
return 0;
}
Function Returning Structure
struct date
{
struct date get_date(void)
int day; {
int month; struct date a;
int year; scanf(%d %d %d,&a.day,&a.month,&a.year);
return a;
};
}
main()
{
struct date d;
struct date get_date(void);
printf(\nEnter date in format day/month/year);
d=get_date();
printf(\nDAte entered by you is %d %d %d\n,d.day,d.month,d.year);
}
Union
Union:
Union is similar as structure. The major distinction between them in
terms of storage.
In structure each member has its own storage location whereas all
the members of union uses the same location.
The union may contain many members of different data type it can
handle only one member at a time union can be declared using the
keyword union.
Hold only one object at a time
Union requires bytes that are equal to the number of bytes required
for the largest members.
Eg: union contains char,int &long then the number of bytes reserved
in the member for the union is 4 bytes.
Union item
{
int m;
float x;
char c;
} code;