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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by stanlvw View Post
    I would love to use strftime but I'm afraid it's not supported in the C programming development suite that I'm using.
    Your development suite doesn't support ISO standardized functions. So god only knows what else it's deficient in.


    Probably a good idea to get a better development environment.

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    That's very likely what I'm doing for my next development project as I don't have time to start over again.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Below shows the code so far after some thinking and collaborations on getting around my problem.

    Code:
    // This function converts day of the week in number to string
    void access_day(unsigned char number, char dayString[])
    {
         // Declaration and Initialization of Days of the Week string
         char sundayArray[] = { 'S', 'u', 'n', 'd', 'a', 'y'};
         char mondayArray[] = { 'M', 'o', 'n', 'd', 'a', 'y'};
         char tuesdayArray[] = { 'T', 'u', 'e', 's', 'd', 'a', 'y'};
         char wednesdayArray[] = { 'W', 'e', 'd', 'n', 'e', 's', 'd', 'a', 'y'};
         char thursdayArray[] = { 'T', 'h', 'u', 'r', 's', 'd', 'a', 'y'};
         char fridayArray[] = { 'F', 'r', 'i', 'd', 'a', 'y'};
         char saturdayArray[] = { 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y'};
         
         // index lookup table of the day strings
         /*uint indexArray[] = {&sundayArray, &mondayArray, &tuesdayArray,
                             &wednesdayArray, &thursdayArray, &fridayArray,
                             &saturdayArray};
         char* pDay = (char*) indexArray[0];*/
         
         char* pIndexArray[] = {&sundayArray, &mondayArray, &tuesdayArray,
                             &wednesdayArray, &thursdayArray, &fridayArray,
                             &saturdayArray};
         char* pDay = pIndexArray[number-1];
         
         
    }
    However, I'm wondering if I should make dayString a pointer instead of a char array and do this:
    Code:
    dayString = pIndexArray[number-1];
    instead of:
    Code:
    char* pDay = pIndexArray[number-1];
    to return the string of the actual day of the week rather than the number.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You're not allowed to return a pointer to a local non-static variable (they'll be destroyed when the function is returned).

    You're over complicating this way too much,

    Code:
    /* returns 0 on success, else non-zero */
    int accessDay(size_t n, char * day, size_t maxLen)
    {
        const char days[7][] = {    "Sunday"
                                    "Monday",
                                    "Tuesday",
                                    "Wednesday",
                                    "Thursday",
                                    "Friday",
                                    "Saturday"
                                };
                                
        /* adjust n, since it's 1 based */
        --n;
                                
        /* error day is out of bounds */
        if(n > (sizeof(days) / sizeof(days[0])))
            return 2;
        
        strncpy(day, days[n], maxLen);
        
        /* etc */
        
        return 0;
    }
    Of course, you'll still need to add error checking -- and it's not perfect either.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Already did that.
    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.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    True enough, that does the trick.
    A little mistake on iMalc's part, then, I guess. Proper C89 code would be:

    Code:
    void getDayOfWeekName(int inDayIndex, char *outDayName)
    {
        static const char* const days[] =
            { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
        assert(1 <= inDayIndex && inDayIndex <= 7);
        assert(outDayName != NULL);
        strcpy(outDayName, days[inDayIndex - 1]);
    }
    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. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I dont know what asserts are but somehow this just looks wrong:
    assert(1 <= inDayIndex && inDayIndex <= 7);

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's not. Just remove the assertion for a second and see for yourself:
    Code:
    #include <assert.h>
    #include <stdio.h>
    #include <string.h>
    
    #define WYSIWYG(maybe) printf( #maybe##", %d\n", (maybe) );
    
    void getDayOfWeekName( int inDayIndex, char *outDayName )
    {
        static const char* const days[] =
            { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
         WYSIWYG( ( 1 <= inDayIndex && inDayIndex <= 7 ) )
        assert(outDayName != NULL);
        strcpy(outDayName, days[inDayIndex - 1]);
    }
    
    int main( void )
    {
        int i;
        char p[10];
    
        for ( i = 1; i <= 7; i++ ) {
            getDayOfWeekName( i, p );
        }
        return 0;
    }
    Last edited by whiteflags; 01-01-2008 at 02:08 PM.

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh yeah I'm dumb >_<

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by mike_g View Post
    I dont know what asserts are but somehow this just looks wrong:
    I guess I confused things by putting that in there, and in the wrong place for C89.
    I write it like this: 1 <= x && x <= 7 because it kinda looks like 1 <= x <= 7, which is pretty much mathematical syntax to show x being between 1 and 7 inclusive. Kinda nifty once you get used to it I think.
    Feel free to write x >= 1 though instead of course.
    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"

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    My preference would be to return a const char*, for two reasons:
    1) any need for an strcpy() is defered to the caller
    2) ability to indicate a bad param error condition (return NULL)

    gg

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would also go with the return a const char* approach since it seems more flexible.
    In many cases you might not want to reassign the actual string you get. You might want to compare it or such. Then a const char* is perfect.
    However, if you really do need to modify the contents or use it in any way, you're free to do the copy yourself. Thus you may save time, as well. New function:

    Code:
    const char* getDayOfWeekName(int inDayIndex)
    {
    	static const char* const days[] = 
    		{ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    	assert(inDayIndex >= 1 && inDayIndex <= 7);
    	return days[inDayIndex - 1];
    }
    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.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yup that's good too IMHO.
    Using const char* as the return value is useful.
    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"

  14. #14
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    Is the assertion used for error-checking to ensure that the number is within range? It seems that mikroC doesn't support that.

    I came up with a way of getting around it such as:

    Code:
    const char* getDayOfWeekName(int inDayIndex)
    {
            static const char* const days[] =
                    { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
    "Saturday" };
            if (!(inDayIndex >= 1 && inDayIndex <= 7)){
               return "Error";
            }
            return days[inDayIndex - 1];
    
    }
    Please let me know what you think. Thanks.
    Last edited by stanlvw; 01-02-2008 at 08:27 PM.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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"

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, 03:07 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM