Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>
#include<stdlib.h>
struct Distance
{
int feet;
float inch;
} dist1, dist2, sum;
int main()
{
struct Distance* distance1 = NULL; 
struct Distance* distance2 = NULL; 
struct Distance* distance3 = NULL; 
distance1 = (struct Distance*)malloc(sizeof(struct Distance*));
distance2 = (struct Distance*)malloc(sizeof(struct Distance*));
distance3 = (struct Distance*)malloc(sizeof(struct Distance*));
printf("input feet for distance no.1:");
scanf("%d", &distance1->feet); printf("input inch for distance no.1:");
scanf("%f", &distance1->inch); printf("input feet for distance no.2:");
scanf("%d", &distance2->feet); printf("input inch for distance no.2:");
scanf("%f", &distance2->inch); printf("input feet for distance no.3:");
scanf("%d", &distance3->feet); printf("input inch for distance no.3:");
scanf("%f", &distance3->inch); printf("1st distance\n");
printf("Enter feet: ");
scanf("%d", &dist1.feet); printf("Enter inch: ");
scanf("%f", &dist1.inch);
printf("2nd distance\n"); printf("Enter feet: ");
scanf("%d", &dist2.feet); printf("Enter inch: ");
scanf("%f", &dist2.inch); // adding feet
sum.feet = dist1.feet + dist2.feet;
// adding inches
sum.inch = dist1.inch + dist2.inch; // changing to feet if inch is greater than 12
while (sum.inch >= 12)
{
++sum.feet;
sum.inch = sum.inch - 12;
} printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch);
return 0;
}


What I have tried:

I'm pretty new to C I hope I could get some hep
Posted
Updated 7-Oct-21 7:00am
Comments
Richard MacCutchan 7-Oct-21 12:35pm    
What exactly are you trying to do? A typedef is merely a compiler shorthand, it does not do anything special.

Like Richard already said, typedef is just syntax sugar.
Anyway, here you are
C
#include <stdio.h>
#include<stdlib.h>

typedef struct 
{
  int feet;
  float inch;
} Distance;
 

Distance dist1, dist2, sum;

int main()
{
  Distance* distance1 = NULL; 
  Distance* distance2 = NULL;
  Distance* distance3 = NULL;
  distance1 = (Distance*)malloc(sizeof(Distance*));
  distance2 = (Distance*)malloc(sizeof(Distance*));
  distance3 = (Distance*)malloc(sizeof(Distance*));
  printf("input feet for distance no.1:");
  scanf("%d", &distance1->feet); printf("input inch for distance no.1:");
  scanf("%f", &distance1->inch); printf("input feet for distance no.2:");
  scanf("%d", &distance2->feet); printf("input inch for distance no.2:");
  scanf("%f", &distance2->inch); printf("input feet for distance no.3:");
  scanf("%d", &distance3->feet); printf("input inch for distance no.3:");
  scanf("%f", &distance3->inch); printf("1st distance\n");
  printf("Enter feet: ");
  scanf("%d", &dist1.feet); printf("Enter inch: ");
  scanf("%f", &dist1.inch);
  printf("2nd distance\n"); printf("Enter feet: ");
  scanf("%d", &dist2.feet); printf("Enter inch: ");
  scanf("%f", &dist2.inch); // adding feet
  sum.feet = dist1.feet + dist2.feet;
  // adding inches
  sum.inch = dist1.inch + dist2.inch; // changing to feet if inch is greater than 12
  while (sum.inch >= 12)
  {
    ++sum.feet;
    sum.inch = sum.inch - 12;
  } printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch);
  return 0;
}

Please note:
each call to malloc is like a sword of Damocles hanging above your head. You should check the returned pointer value before actually use it (malloc may fail) and, at the end you should release all the successfully allocated memory (see free - cppreference.com[^]).
 
Share this answer
 
With a struct, typedef just creates a "more friendly" name for a struct, to prevent you having to use the full name each time:
C++
typedef struct 
    {
    int feet;
    float inch;
    } Distance;
Distance dist1, dist2, sum;
int main()
    {
    dist1.feet = 3;
...
    return 0;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900