Function converting from 1-7 for days of the week to actual strings

This is a discussion on Function converting from 1-7 for days of the week to actual strings within the C Programming forums, part of the General Programming Boards category; assert is a debug-only check that breaks you into the debugger at the point where the statement was false. On ...

  1. #31
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    assert is a debug-only check that breaks you into the debugger at the point where the statement was false.
    On an embedded system I guess you'd need to implement your own form of assert, that perhaps just logs the error. What you've written could be fine for you.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #32
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I have another question:

    Given the function declaration of the number to string conversion being:

    Code:
    const char* getDayOfWeekName(int inDayIndex);
    and the LCD string output function being:

    Code:
    void LCD4_OUT(char* outstr);
    I'm pretty bad at signatures. I'm not sure how to put the LCD4_OUT function call using the return value of the getDayOfTheWeek function properly. I have tried:

    Code:
    LCD4_OUT(getDayOfWeekName((int) access_day_number()));
    also
    Code:
    char* dow;
    dow = &(getDayOfWeekName((int) access_day_number()));
    LCD4_OUT(dow);
    I got an error of operands not operable on the function call with the ampersand or illegal pointer conversion. Any help would be much appreciated.

  3. #33
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    char * dow;
    dow = getDayOfWeekName((int) access_day_number());
    LCD4_OUT(dow);
    You've already got the address...

  4. #34
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Disapprove. If case of error, return NULL (ie, an error occurred and no pointer could be returned).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't return a NULL pointer on error, return Saturday -- Because everyone loves saturdays!

  6. #36
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    That would be confusing. That would tell the caller the function succeeded. I hope you're sarcastic on that one...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #37
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In this case, I would say that having an assert in the code to check the value, combined with returning a "unknown" string if it's not a valid value would be the right thing to do. Returning a NULL is ok-ish, but if it's not checked in the calling code, it's likely to just make things worse than a "Unknown" value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #38
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    True, but it's the caller's responsibility to check if it failed.
    And returning an "error string" is just poor programming IMO. Computers don't deal with strings. They deal with numbers. Returning an error code or a struct or class with error information is much more informative than a string.
    If such is the case, do the usual pass in an extra argument to receive if it failed or not, or to get the string and to return an error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I guess it depends on how you want to use it, and what the consequences are. Returning NULL can cause the app to crash [in fact, it probably SHOULD crash if it tries to access the NULL pointer], returning a "Unknown" will just cause "strange" results for the user - which is preferable in some cases - particularly if the system is intended to run for a long time. It does mean that the error checking is harder - but that's why I suggested having an assert in the function itself.

    In fact, if it's critical that the "invalid day" is properly captured, I would suggest that putting code in to "crash out" inside the GetDayOfWeek, rather than propagating the error back to the calling function - it doesn't really help to propagate it back down, as that will only make it harder to see what's going wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #40
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    An assert is always good to track down simple mistakes such as this one. The application should never call the function with an invalid value.
    I think in this case you can probably get away with just an assert since you are responsible for passing correct information to the function.
    Otherwise I would definitely place an an assert and return NULL, or add an argument to the the string and return success or failure. In that case, I could easily return "Unknown" and get away with it since I wouldn't be comparing the string itself for errors, but the return value.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #41
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I already tried using an assert, but unfortunately it's not supported. I'm still not too sure how to reference the pointers properly to get the LCD to display the string on the days of the week text.

  12. #42
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by stanlvw View Post
    I'm still not too sure how to reference the pointers properly to get the LCD to display the string on the days of the week text.
    What went wrong with what zacs7 posted earlier? Your getDayofWeekName function returns a char*, which is exactly what your output function wants.

  13. #43
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    I got an error of "illegal pointer conversion: [] to [dow]"

  14. #44
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by stanlvw View Post
    I got an error of "illegal pointer conversion: [] to [dow]"
    I don't know enough about your compiler to know exactly what that's telling you. What line in your code gives that error?

  15. #45
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    The second line the code in zacs7's post.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21