-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraction.c
38 lines (34 loc) · 1.44 KB
/
fraction.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
struct Fraction{
int top_; //top_ is a member of Fraction
int bot_; //bot_ is a member of Fraction
};
void multiplyFraction(const struct Fraction* first,
const struct Fraction* second,
struct Fraction* result);
int main(void){
struct Fraction firstValue; //firstValue is an instance of Fraction
//or in other words a variable of type
//Fraction
struct Fraction secondValue;
struct Fraction result;
firstValue.top_=1;
firstValue.bot_=4;
secondValue.top_=2;
secondValue.bot_=3;
multiplyFraction(&firstValue,&secondValue,&result);
printf("result of 1/4 * 2/3 = %d/%d\n",result.top_,result.bot_);
}
/*When passing instances of structs to a function, the general practice
is to pass their addresses instead of a copy of the entire struct.
This is because structs are generally large and a copy would mean
copying all the data members over to a second location in memory which
can have significant cost associated with it in terms of both time
and memory*/
void multiplyFraction(const struct Fraction* first,
const struct Fraction* second,
struct Fraction* result){
///ewwwwwww: (*result).top_=(*first).top_*(*second).top_;
result->top_=first->top_*second->top_;
result->bot_=first->bot_*second->bot_;
}