structures-and-union
structures-and-union
Syntax :
struct structurename variable1, variable2, ..... variable n;
Example
Struct book b1; /* b1 is a structure variable name */
Syntax :
Stru_variablename.membername;
Example
b1.pages=400;
b1.price=450.00;
Write a Program the create the Account Details using Structure?
#include<stdio.h>
struct amount
{
int ac_no;
char name[10];
int balance;
};
void main()
{
struct amount v;
printf("\nEnter the account Details");
scanf("%d%s%d",&v.ac_no,&v.name, &v.balance);
printf("The values are %d%s%d",v.ac_no,
v.name, v.balance);
printf("%d",sizeof(struct amount));
}
Write a program to create student structure
#include<stdio.h>
struct stud
{
int id;
char name[20];
int mark1,mark2,mark3;
int total;
int avg;
}b;
void main()
{
printf("\nEnter the student details");
scanf("%d %s %d %d %d",&b.id,&b.name,
&b.mark1,&b.mark2,&b.mark3);
b.total=b.mark1+b.mark2+b.mark3;
b.avg=b.total/3;
printf("%d %s %d %d %d",b.id,b.name, b.mark1, b.mark2,
b.mark3);
}
Array of structures are defined as a group of data items
of different data types stored in a consecutive memory location
with a common variable name.
For Example,
struct employee
{
int empno;
char empname[20];
char deptname[10];
float salary;
}emp[5];
Passing Structures as Arguments
A structure variable can be passed as an argument to
another function
Example
Struct date
{
int month;
int day;
int year;
};
struct account
{
int acc_no;
char name[40];
char acc_type;
float balance;
struct date dob;
} customer;
/* NESTED STRUCTURES
A structure within another
scanf("%d",&s.sno);
structure */
printf("enter the sname\n");
#include<stdio.h>
scanf("%s",&s.name);
struct dob {
printf("enter the dob\n");
int date;
scanf("%d%d%d %c",&s.sdob.date,
int month;
&s.sdob.month,&s.sdob.year,&s.sex);
int year; };
printf("%d\t %s\t %d %d %d \t
struct stud{
%c", s.sno, s.sname, s.sdob.date,
int sno;
s.sdob.month, s.sdob.year, s.sex);
char sname[10];
getch();
struct dob sdob;
}
char sex; };
void main()
{
struct stud s;
clrscr();
printf("enter the sno\n");
Session Summary
The keyword “struct” begins with the structure definition followed by the tag(i.e
structure name) within the braces where structure members are declared
The structure definition create a new data type that can be used to declare variables.
A member of structure is always referred and accessed using the member access
operator or dot operator (.) via the structure variable name
A structure containing a member that is a pointer to the same structure type is called
as a self referential structure.
The structure pointer operator () is used when accessing a structure member via a
pointer to the structure.
Discuss Unions
Create Union variable
Access Union member using . operator
Discuss Unions of Structures
Compare Structure and Array
Difference between structure & union
Unions are collection of data items of different
datatype grouped together under a single variable
name for convenient handling. Unions permits several
data items to be stored in the same portion of
computers memory. But, Only one member of union
can be assigned a value at any one time.
syntax : union <union name > {
datatype member1;
datatype member2; };
union book {
int pages;
char bookname[10];
char author[20];
float price;
};
#include <stdio.h>
union unionJob { //defining a union
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{ char name[32]; float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes",
sizeof(sJob));
return 0; OUTPUT
} size of union = 32 bytes
Structure Arrays
It is an single entity representing a It is an single entity representing a
Collection of data types of different Collection of data types of same data
data types types
Individual entries in a structure are Individual entries in a array are called
called Members array elements
The members of a structure are not The members of a array are stored in
stored in sequence of memory sequence of memory locations
locations
All the members of a structure can be Only the first member of an union can be
initialized initialized
Individual structure elements are Individual array elements are accessed by
referred through the use of .(dot or its name followed by the square braces[]
membership) operator within which the index is placed
Initialization of elements can be done Initialization of elements can be done
only during structure definition during array declaration
Structure Union
Keyword : struct Keyword : union
Each member in a structure occupies All the members of a union use the same
and uses its own memory space memory space
More memory space is required since Less memory space is required since all
each member is stored in a separate member is stored in the same memory
memory locations locations
All the members of a structure can be Only the first member of an union can be
initialized initialized
The variables that make up the The variables that make up the union are
structure are called Structure called union variable
variable
Individual structure elements are Individual union elements are referred
referred through the use of .(dot or through the use of .(dot or membership )
membership) operator operator
Session Summary
The memory space reserved for a union member is large enough to store its largest
member.
The keyword “union” begins the union declaration followed by the tag (i.e., union name)
Within the braces union members are declared.