• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How do I return false value from conditional statement inside a struct function ?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I make program in C. I tried to return false value from a conditional statement inside a struct function. This function takes input from another struct function, and if returns true it needs to return multiple float values to be used for the next function. Here's my code :

What I have tried:

  • Include stdbool.h to use true/false, caught error : returning 'int' from a function with incompatible result type 'struct Coffee'
  • Manually define true false using #define TRUE 1 and #define FALSE 0, caught the same error
  • using return 1; caught the same error

  • Any advice ?
     
    author
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If the return type of your function is "struct Coffee", then all return statements must return a value of type struct Coffee. The "failure" return path must therefore specify its failure in the return type itself, e.g. with special values, or via another means such as errno or by adding an extra function parameter which is a pointer to a success/failure value.
     
    Silvi Hasana
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Anthony Aj Williams wrote:If the return type of your function is "struct Coffee", then all return statements must return a value of type struct Coffee. The "failure" return path must therefore specify its failure in the return type itself, e.g. with special values, or via another means such as errno or by adding an extra function parameter which is a pointer to a success/failure value.



    Hi, I still don't quite get it, also I forgot to include struct Coffee, here it is :
    Earier I also I tried with adding to my struct  :
    Then in my function I added :
    But I encountered the same error.
     
    Anthony Aj Williams
    author
    Posts: 60
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You need



    on the failure path and



    on the success path.

    You can't implicitly convert from an integer or boolean to a struct in C.
     
    Silvi Hasana
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Anthony Aj Williams wrote:You need



    on the failure path and



    on the success path.

    You can't implicitly convert from an integer or boolean to a struct in C.



    I see. Thank you, it works.
     
    Ranch Hand
    Posts: 117
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Anthony Aj Williams wrote:You need


    or:

    on the failure path and


    or:
    Not saying that's much/any better; just another way of doing it if the OP wasn't aware.

    BTW looks like the Coffee.ret field should be bool (#include stdbool.h) with values true/false.
     
    John Matthews
    Ranch Hand
    Posts: 117
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    However rather than have the ret field in Coffee, a better solution would be to remove it and use something like:
     
    Anthony Aj Williams
    author
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, that's more idiomatic C: the return value indicates success/failure (and possibly an error code for failure if you return int), while the struct to hold the output data is passed in by pointer.
     
    Silvi Hasana
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    John Matthews wrote:However rather than have the ret field in Coffee, a better solution would be to remove it and use something like:



    This is indeed the most efficient way. I wasn't aware of this one too, thank you I learn a lot.
     
    Bartender
    Posts: 20849
    125
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And now that you've got you logic cleaned up, some suggestions about general style!



    Notice the extra braces I added? I learned to do that after too many time doing something like this:


    Which actually doesn't compile, but if the "else" wasn't there, it would compile just fine, not execute correctly, and waste me a day looking for the problem (the most obvious ones are always the hardest to catch). So the extra typing is well worth the effort.

    Beyond that, "x" and "y" don't mean anything to me as part of a "coffee" class. Are there more meaningful names you could use?
     
    Silvi Hasana
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Holloway wrote:And now that you've got you logic cleaned up, some suggestions about general style!



    Notice the extra braces I added? I learned to do that after too many time doing something like this:


    Which actually doesn't compile, but if the "else" wasn't there, it would compile just fine, not execute correctly, and waste me a day looking for the problem (the most obvious ones are always the hardest to catch). So the extra typing is well worth the effort.

    Beyond that, "x" and "y" don't mean anything to me as part of a "coffee" class. Are there more meaningful names you could use?



    Hi thanks !! I was aware of this one since I came across many times as well. Also, it's actually exactly what I did after received answer from John Matthews. I needed to add some extra lines, so I added the braces.  
    For the X and Y, they are amount of different ingredients needs to be added. I'm not the best at naming variable, it's not very descriptive and makes me run into trouble too sometimes (try to recall which one is which, for example). I usually in a rush and just write the first thing pops up in my head, I'll work on this naming variable better, thanks again !!
     
    John Matthews
    Ranch Hand
    Posts: 117
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Holloway wrote:Notice the extra braces I added? I learned to do that after too many time doing something like this:
    Which actually doesn't compile, but if the "else" wasn't there, it would compile just fine

    It wouldn't if you were using gcc with basic (should be the minimum you use) options -Wall -Werror, although it does need to be gcc 6 (released 2016):
    https://developers.redhat.com/blog/2016/02/26/gcc-6-wmisleading-indentation-vs-goto-fail/

    I do agree that in general you should code defensively, although that might conflict with the coding guidelines you are having to follow. I've previously worked in environments where open/close braces were mandatory, but in the DPDK guidelines we're currently following they aren't:
    https://doc.dpdk.org/guides/contributing/coding_style.html#control-statements-and-loops
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!