I'm trying to create a program which reads a number as type fraction using procedures. That's my code (the compiler gives me an error in readFraction() line 9):
#include <stdio.h>
typedef struct {
int num,den;
} fraction;
void readFraction(fraction * f) {
printf("Insert a fraction: ");
scanf("%d/%d", &*f.num, &*f.den);
}
void writeFraction(fraction f) {
printf("Fraction inserted: %d/%d\n", f.num, f.den);
}
void main() {
fraction a;
readFraction(&a);
writeFraction(a);
}
This is the error message:
$ gcc fraction.c
fraction.c: In function ‘readFraction’:
fraction.c:9:21: error: ‘f’ is a pointer; did you mean to use ‘->’?
9 | scanf("%d/%d", &*f.num, &*f.den);
| ^
| ->
fraction.c:9:30: error: ‘f’ is a pointer; did you mean to use ‘->’?
9 | scanf("%d/%d", &*f.num, &*f.den);
| ^
|
I know I could solve this using something like readFraction(&a.num,&a.den);
or a=readFraction();
and the function returns the fraction but I'd like to know if I could just pass the fraction as a parameter.