# Thread: Pointers to structs

1. ## Pointers to structs

I have this program to write that will add two fractions input by the user. I need to do this using pointers to structs. I have the code written, but it will not run all the way through. Could someone help me debug this thing and get it running properly.

#include <stdio.h>

typedef struct
{
int numerator;
int denominator;
}FRACTION;

void Add(FRACTION *pfrac1, FRACTION *pfrac2);
void Display(FRACTION *pres);

int main()
{
FRACTION fr1;
FRACTION fr2;
FRACTION result;

FRACTION *pfrac1;
FRACTION *pfrac2;
FRACTION *pres;

pfrac1 = &fr1;
pfrac2 = &fr2;
pres = &result;

printf("Please enter the first fraction in the form x/y: ");
scanf("%d /%d", pfrac1->numerator, pfrac2->denominator);
printf("Please enter the second fraction in the form x/y: ");
scanf("%d /%d", pfrac2->numerator, pfrac2->denominator);
Display(&result);
getchar();
getchar();
}

void Add(FRACTION *pfrac1, FRACTION *pfrac2)
{
FRACTION result;
FRACTION *pres;

pres = &result;

pres->numerator = (pfrac1->numerator * pfrac2->denominator) + (pfrac2->numerator * pfrac1->denominator);
pres->denominator = (pfrac1->denominator * pfrac2->denominator);
}
void Display(FRACTION *pres)
{
printf("\nThe result is %d/%d", pres->numerator, pres->denominator);
}

2. ## Addendum to pointers to structs

I would just like to add that I would also like to omit the statements in "main" to obtain the input from user and create a function called "GetInput" that would handle that so that "main" doesnt look "messy" if you will.

3. You need the address of numerator and denominator:

scanf("%d /%d", &pfrac1->numerator, &pfrac2->denominator);

Also use code tags when posting code.

Popular pages Recent additions