2-4-21

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Eg.

preparing dmart bill


#include<stdio.h>

#include<conio.h>

void dummy(float a)

float *p=&a;

struct bill

char item[20];

float qty, price;

}b[100];

void main()

int i=0, c=1;

char ch;

float amount, tot=0;

clrscr();

while(1)
{

flushall();

printf("Enter product name "); gets(b[i].item);

printf("Enter quantity"); scanf("%f",&b[i].qty);

printf("Enter price ");scanf("%f",&b[i].price);

printf("R u want to continue [y/n] ");

flushall();

scanf("%c",&ch);

if(ch=='y'||ch=='Y')

i++; c++;

else break;

puts("\t\t\t DMART");

puts("\t\t\t SANATH NAGAR");

puts("--------------------------------------------------------------");

printf("%-20s%10s%10s%10s\n","ITEM","QTY","PRICE","AMOUNT");

puts("--------------------------------------------------------------");

for(i=0;i<c;i++)
{

amount=b[i].qty * b[i].price;

tot=tot+amount;

printf("%-20s%10.2f%10.2f%10.2f\n",b[i].item, b[i].qty, b[i].price,


amount);

puts("------------------------------------------------------------");

printf("%40s%10.2f\n","Total Amount:",tot);

printf("***** Thank You - Visit Again *****");

getch();

}
Eg.POINTER STRUCTURE MEMBERS
Eg.
pointer to structure / Pointer structure variables
#include<stdio.h>
#include<conio.h>
void dummy(float a)
{
float *p=&a;
}
struct
{
char author[20],name[20];
float price;
}b, *p;
void main()
{
p=&b;
clrscr();
printf("Enter author name "); gets((*p).author);
printf("Enter book name "); gets(p->name);
printf("Enter price "); scanf("%f",&p->price);
printf("Author: %s\n",p->author);
printf("Book: %s\n",p->name);
printf("Price: %.2f",(*p).price);
getch();
}
Note:
Here * have less priority than . operator. Hence we
have to use ()
*. Or  is called pointer to member access
operator
Dynamic structures: allocating memory to
structure variables at runtime. It prevents
memory wastage and improves the program
performance. It need pointer structure
variables.
#include<stdio.h>
#include<conio.h>
#include<alloc.h> #include<stdlib.h>
void dummy(float a)
{
float *p=&a;
}
struct emp
{
int id;
char name[20];
float sal;
}e, *p;
void main()
{
int i=0,c=1; char ch;
clrscr();
p = (struct emp *)calloc(c,sizeof(e));
while(1)
{
printf("Enter emp id, name, salary ");
scanf("%d %s %f",&(p+i)->id,(p+i)-
>name,&(p+i)->sal);
flushall();
printf("Do U Want to Continue [y/n] ");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{
i++; c++;
p=(struct emp *)realloc(p, c*sizeof(e));
}
else break;
}
puts("Id\tName\tSal");
puts("--------------------------------------");
for(i=0;i<c;i++)
printf("%d\t%s\t%.2f\n",(p+i)->id, (p+i)->name,
(p+i)->sal);
free(p);
p=NULL;
getch();
}

You might also like