-1
typedef struct{
 int num, den;
} tfrac;

tfrac multf(tfrac a, tfrac b);

tfrac multf(tfrac a, tfrac b){
  tfrac res;
  res={a.num*b.num, a.den*b.den}; //The ERROR is here
  return res;
}

This program multiplies fractions. I don't know why I have an error in the function.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 4
    What is `res={a.num*b.num, a.den*b.den};` supposed to do? It's certainly not valid C. – Robert Harvey Dec 07 '18 at 23:26
  • what datatype is `res` ? – brokenfoot Dec 07 '18 at 23:26
  • @brokenfoot: `tfrac`. – Robert Harvey Dec 07 '18 at 23:27
  • Is there some documentation or example code you saw that lead you to believe that line would work? – David Schwartz Dec 07 '18 at 23:29
  • 1
    What/who lead you to belive that _typedefing_ a struct would be a good idea?, not a rhetorical question, I'm genuinely curious. – Jenny T-Type Dec 07 '18 at 23:32
  • In the excercise they obligated me to use that struct. – Alex Hernandez Dec 07 '18 at 23:35
  • How can I solve this? :( – Alex Hernandez Dec 07 '18 at 23:42
  • 3
    @JennyT-Type typedef'ing structs is very common and idiomatic in C. Why are you asking that question? – Barmar Dec 07 '18 at 23:50
  • @Barmar Why?, curiosity?. I don't want to assume, I would do that (_typedefing_)to avoid declaring something like `struct mydata_type mysdata_var;` and go for `mydata_type mydata_var` saving me from typing the word `struct` again and some (tiny) disk space along the way, But then I would get into trouble if I forget I'm passing a struct instead of a union or an malloc'ed chunk, so if I would typedef I would have something like `typedef struct s_mydata_type {/* members */} s_mydata_type_t;` then, `typedef union u_myotherdata_type {/* mxmembers */} u_myotherdata_type_t` as reminders (cont). – Jenny T-Type Dec 08 '18 at 13:56
  • (cont'd), but then, some programmers would chastise me about using prefixes (and suffixes) reserved for some future use by the C standard, So i found that refraining from typedefing saves me from using my data in an incorrect way (eg, assigning to members of an union as they were struct members -- not exactly trivial to pinpoint if compiled correctly), and from future concerns. With that being said, I sometimes typedef my structs and/or unions, if, for example, I'm going to use several variables of that type, or complex data structures from an specific type. – Jenny T-Type Dec 08 '18 at 14:09

2 Answers2

0

An initializer is a part of a declarator. So you can't declare your variable on one line and then assign values to it with an initializer on another line. Instead:

tfrac res={a.num*b.num, a.den*b.den};
mnistic
  • 10,866
  • 2
  • 19
  • 33
0

The syntax you are using is not valid in C. You can get pretty close to this syntax in context of assignment operator by using a compound literal

tfrac multf(tfrac a, tfrac b)
{
  tfrac res;
  res = (tfrac) { a.num * b.num, a.den * b.den };
  return res;
}

Note the (tfrac) part before the {}.

But in your case you can simply use initialization instead of assignment

tfrac multf(tfrac a, tfrac b)
{
  tfrac res = { a.num * b.num, a.den * b.den };
  return res;
}

However, returning to compound literals again, if you prefer, you can use a compound literal to turn the whole thing into an one-liner

tfrac multf(tfrac a, tfrac b)
{
  return (tfrac) { a.num * b.num, a.den * b.den };
}
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765