Hi there. I have some code to add fractions.
#include <stdio.h>
#include <stdlib.h>
struct frac
{
int enumerator;
int denominator;
};
typedef struct frac frac_t;
frac_t *Add(frac_t *b1, frac_t *b2)
{
frac_t rfrac;
frac_t *p;
p = &rfrac;
(*p).enumerator= ((*b1).enumerator* (*b2).denominator) + ((*b2).enumerator* (*b1).denominator);
(*p).denominator= ((*b1).denominator* (*b2).denominator);
return p;
}
int main(void)
{
frac_t b1 = {2,4};
frac_t b2 = {1,7};
frac_t *add = Add(&b1, &b2);
printf("%i %i\n", add->enumerator, add->denominator);
system("pause");
return 0;
}
This works perfectly fine. The result is: 3 5, as it is supposed to be.
If I add a "printf" it totally messes up my result:
int main(void)
{
frac_t b1 = {2,4};
frac_t b2 = {1,7};
frac_t *add = Add(&b1, &b2);
printf("addition:\n");
printf("%i %i\n", add->enumerator, add->denominator);
system("pause");
return 0;
}
The result is:
addition:
2008958704 -1
What went wrong?