Thread: global const string vector

  1. #1
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57

    global const string vector

    Hello there,
    I need a const global string table like this:
    Code:
    const char* dayOfWeekStrings[7] =
    {
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday"
    };
    At the moment this declaration is in my c file and in the header i have:
    Code:
    extern const char* dayOfWeekStrings[7]
    And it works. But I thought why not declare it in the header only and so i removed the extern and moved the declaration to header with static:
    Code:
    static const char* dayOfWeekStrings[7] =
    {
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday"
    };
    But now the compiller warns me that that logLevelStrings is unused even though it is and it is compiling. Is it a correct aproach? I would apreciate help.
    Last edited by bremenpl; 12-19-2015 at 04:11 AM.

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by bremenpl View Post

    But now the compiller warns me that that logLevelStrings is unused even though it is and it is compiling. Is it a correct aproach? I would apreciate help.
    Didn't you intend to move 'dayOfTheWeek' to the header file instead of 'logLevelStrings'. Seems like you've moved the wrong struct definition.

    Other than that, I personally don't have a problem with you moving the struct definition into the header file.

    Also, looking at 'logLevelStrings', I would use an 'enum' to represent them other than strings.

  3. #3
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57
    I have edited my post, I pasted wrong struct, thank you.

    I have an enum that represens days as numbers. that way I can reffer to them as indexes and do:
    Code:
    printf("the day is %s", dayOfWeekStrings[0]);
    And it would print:
    Code:
    the day is Monday"
    So static const definition in header file should be ok?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bremenpl
    So static const definition in header file should be ok?
    The thing is, a static variable at file scope is local to the translation unit. This is great in a source file, but in a header this means that a different variable of the same name becomes present in every source file that includes the header. For a static constant at file scope, maybe the compiler can determine that since it is a constant, it can merge them together anyway, but if not you actually have unnecessary copies of the same global constant.

    Oh, and note that while you have an array of pointers of const char, the pointers themselves are not constant and so can be modified. Therefore, dayOfWeekStrings is a global variable rather than a global constant, and global variables should be avoided. You can avoid this with:
    Code:
    const char* const dayOfWeekStrings[7] =
    {
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday"
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User bremenpl's Avatar
    Join Date
    Apr 2013
    Posts
    57
    Thank you for the const hint, you are right i missed it!

    As for the static- what would you say then, is the proper method of defining this const "structure" of data? I need this in the source file of the header, but also in other files including this header. I was wondering about creating a getter function for it, but it seemd to much for me. Could you please advice the proper way from designers point of view?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Using extern in the header declaration was appropriate. The source file where you implement the functions you use would then define this array with its content in global scope.

    dates.h
    Code:
    #ifndef JCK_HEADER
    #define JCK_HEADER
    
    extern const char *const dayOfWeekStrings[7];
    
    #endif
    dates.c
    Code:
    #include "dates.h"
    
    const char* const dayOfWeekStrings[7] =
    {
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday"
    };
    
    /* ... */
    main.c
    Code:
    #include <stdio.h>
    #include "dates.h"
    
    int main()
    {
        size_t i;
        for (i = 0; i < sizeof(dayOfWeekStrings) / sizeof(dayOfWeekStrings[0]); ++i)
        {
            printf("%-15s", dayOfWeekStrings[i]);
        }
        putchar('\n');
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign <char>vector to global std::string
    By Ducky in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2015, 10:54 PM
  2. vector<vector<const char*>> behaving strangely
    By unixmania in forum C++ Programming
    Replies: 6
    Last Post: 07-26-2013, 02:50 PM
  3. Help creating global const char[] to store users home dir
    By KoRn KloWn in forum C Programming
    Replies: 4
    Last Post: 10-06-2012, 02:47 PM
  4. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  5. Global memory optimizations of const char*
    By Mario F. in forum C++ Programming
    Replies: 14
    Last Post: 08-02-2007, 02:46 PM

Tags for this Thread