Array of structure variables:
Any structure variable is capable of storing the data
belongs to a single entity.
If we want to process the data belongs to number of entities
then we can create an array of structure variables rather creating individual
variables.
Example:
struct employee
{
int empno;
char name[50];
int sal;
float
hra,da,gross;
};
struct employee x[5];
Specification3:
Accept the details of "n" products and print the
bill.
Program:
#include<stdio.h>
struct product
{
int sno;
char pname[50];
float price;
int qty;
float tot;
};
void main()
{
struct product
x[50];
int i,n;
float bill,temp;
clrscr();
printf("How
many products?");
scanf("%d",&n);
printf("Enter
the details of %d products:\n",n);
for(i=0;i<n;i++)
{
x[i].sno=i+1; /* storing sno automatically which is 1 more
than index*/
printf("\nName:");
scanf("%s",x[i].pname);
printf("Price:");
scanf("%f",&temp);
x[i].price=temp;
printf("Quantity:");
scanf("%d",&x[i].qty);
x[i].tot=x[i].price*x[i].qty;
}
printf("\n\n
XYZ SUPER BAZAR\n");
printf(" Trunk road,
Kavali-524201\n");
printf("-----------------------------------------------------------------\n");
printf("Sno Name Price Qty Total \n");
printf("-----------------------------------------------------------------\n");
bill=0;
for(i=0;i<n;i++)
{
printf("%d\t%s\t\t%f\t%d\t%f\n",x[i].sno,x[i].pname,x[i].price,x[i].qty,x[i].tot);
bill=bill+x[i].tot; /* Adding
totals to bill */
}
printf("
-----------------\n");
printf(" %f\n",bill);
printf("
-----------------\n");
getch();
}
Execution:
How many products?3
Enter the details of 3 products:
Name:Burger
Price:85
Quantity:2
Name:Pastha
Price:120
Quantity:1
Name:Pizza
Price:250
Quantity:2
XYZ
SUPER BAZAR
Trunk
road, Kavali-524201
-----------------------------------------------------------------
Sno Name Price Qty Total
-----------------------------------------------------------------
1 Burger 85.000000 2
170.000000
2 Pastha 120.000000 1
120.000000
3 Pizza 250.000000 2
500.000000
-----------------
790.000000
-----------------
Example explained:
Accepted the details of "n" products from keyboard
and stored into array of structure variables.
Printed the details of all products purchased including with
bill as output.
Structure within another structure:
A structure can be defined within another structure.
The memory allocation of inner structure variable allocates
automatically on creating the outer structure variable.
Members of inner structure variable can be accesses through
direct member access operator.
Example:
#include<stdio.h>
struct employee
{
int empno;
char ename[50];
struct
{
int dd;
int mm;
int yyyy;
}date;
int sal;
float hra,da,gross;
};
void main()
{
struct employee x;
clrscr();
printf("Empno:");
scanf("%d",&x.empno);
printf("Employee Name:");
scanf("%s",x.ename);
printf("Date
of joining:(dd/mm/yyyy)");
scanf("%d/%d/%d",&x.date.dd,&x.date.mm,&x.date.yyyy);
printf("Salary:");
scanf("%d",&x.sal);
x.hra=(float)x.sal*13/100;
x.da=(float)x.sal*15/100;
x.gross=x.sal+x.hra+x.da;
printf("\nEmployee Details:\n");
printf("Empno:%d\nEmployee
Name:%s\n",x.empno,x.ename);
printf("Date
of joining:%d/%d/%d\n",x.date.dd,x.date.mm,x.date.yyyy);
printf("Salary:%d\nHRA:%f\nDA:%f\nGross:%f",x.sal,x.hra,x.da,x.gross);
getch();
}
Execution:
Empno:1000
Employee Name:Smith
Date of joining:(dd/mm/yyyy)01/10/2005
Salary:5500
Employee Details:
Empno:1000
Employee Name:Smith
Date of joining:1/10/2005
Salary:5500
HRA:715.000000
DA:825.000000
Gross:7040.000000
Example explained:
Inner struct has no name but its instance is created with
the name "date"
The members of inner struct can be accessed using <outer
variable name>.<inner variable name>.<name of member>
Sending a structure variable as an argument:
Like a variable, an array even a structure variable can be
sent as an argument.
Here both the actual and formal arguments must be of struct
type.
Even the type must be specified with the function
declaration statement (proto type)
Example:
struct book
{
char name[50];
char author[50];
char publisher[50];
float price;
};
void display(struct book);
void main()
{
struct book
x={"Let us c","Kanithkar","BpB",275};
clrscr();
display(x);
getch();
}
void display(struct book a)
{
printf("Book
Name:%s",a.name);
printf("\nAuthor: %s",a.author);
printf("\nPublisher: %s",a.publisher);
printf("\nPrice:%f",a.price);
}
Output:
Book Name:Let us c
Author: Kanithkar
Publisher: BpB
Price:275.000000
Example explained:
Here the struct variable "x" is send as an
argument to the function display().
All the members of "x" are assigned to the formal
argument "a"
Pointer to a structure variable:
Pointer is a variable which stores the address of another
variable.
A pointer can be defined to store the address of a structure
variable but which must be of same structure type.
Even pointer to structure variable takes two bytes.
Pointer to structure variable can access the members of
structure variable using either *. and -> (pointer to) operators.
Example:
struct circle
{
int rad;
float area,cir;
};
struct circle x,*p=&x;
Note: Here *p is kept in ()
to provide explicit priority to value at operator
Program:
#include<stdio.h>
#define PI 3.14
struct circle
{
int rad;
float area,cir;
};
void main()
{
struct circle
x,*p=&x;
clrscr();
printf("Enter
the radios:");
scanf("%d",&(p->rad));
p->area=PI*(p->rad)*(p->rad);
p->cir=2*PI*(p->rad);
printf("Area
%f",p->area);
printf("\nCircumference %f",p->cir);
getch();
}
Execution:
Enter the radios:15
Area 706.500000
Circumference 94.199997
Sending the address of a structure variable as an argument:
We can send the address of a structure variable as an
argument into any function.
Here the formal argument must be a pointer to the structure
variable.
Now formal argument can refer the members of actual argument
using either *. or -> operators.
Example:
#include<stdio.h>
#define PI 3.14
struct circle
{
int rad;
float area,cir;
};
void calculate(struct circle*);
void main()
{
struct circle x;
clrscr();
printf("Enter
the radios:");
scanf("%d",&x.rad);
calculate(&x);
printf("Area
of circle %f",x.area);
printf("\nCircumference of circle %f",x.cir);
getch();
}
void calculate(struct circle *p)
{
p->area=PI*(p->rad)*(p->rad);
p->cir=2*PI*(p->rad);
}
Execution:
Enter the radios: 17
Area of circle 907.460022
Circumference of circle 106.760002
Example explained:
"x.rad" is accepted from the keyboard
The address of "x" is send to the function
calculate().
Using pointer "p" we could directly access the
members of "x" using -> operator.
0 comments:
Post a Comment