EmbLogic's Blog

Structures using pointers

#include”header1.h”

struct distance *input()
{
struct distance *d1;
d1=(struct distance *)malloc(sizeof(struct distance));
printf(“\nenter the distance in feet”);
scanf(“%d”,&d1->feet);
printf(“\nenter the distance in inches”);
scanf(“%f”,&d1->inches);
return d1;
}

struct distance *add(struct distance *d1, struct distance *d2)
{
struct distance *d4;
d4=(struct distance *)malloc(sizeof(struct distance));
d4->feet=d1->feet+d2->feet;
d4->inches=d1->inches+d2->inches;
if(d4->inches>=12)
{
d4->feet++;
d4->inches=d4->inches-12;
}
return d4;
}

struct distance display(struct distance *d1, struct distance *d2,struct distance *d3)
{
printf(“the distance(%d’ -%f\”)and the distance (%d’-%f\”) (%d’-%f\”)”,d1->feet,d1->inches,d2->feet,d2->inches,d3->feet,d3->inches);
}

int main()
{
struct distance *d1,*d2,*d3;
d1=(struct distance *)malloc(sizeof(struct distance));
d2=(struct distance *)malloc(sizeof(struct distance));
d3=(struct distance *)malloc(sizeof(struct distance));
d1=input();
d2=input();
d3=add(d1,d2);
display(d1,d2,d3);
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>