Thread: static char array

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    static char array

    Hello

    I need a static string array in my program for instance:

    Code:
    char *day[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    Is there any better/other way to do this in c++, or I have to do it the same as in c?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include <string>
    
    std::string days[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    ... or depending on how it's used
    Code:
    enum days { SUN, MON, TUE, WED, THU, FRI, SAT };
    ... but of course that option also works in C, as well.
    Sent from my iPadŽ

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Or:
    Code:
    char day[7][4]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    I am not sure \0's are needed but I added them so there can be no problem with the code.
    Last edited by maxorator; 10-08-2006 at 07:03 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    They're not needed. On the contrary, you just created compile-time buffer overflows, and a good compiler would refuse to compile it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM