Worksafe Array of Strings

This is a discussion on Worksafe Array of Strings within the C Programming forums, part of the General Programming Boards category; Hi I want to have a list of thousands of strings as one array. Last time I used to do: ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    68

    Worksafe Array of Strings

    Hi

    I want to have a list of thousands of strings as one array.
    Last time I used to do:
    Code:
    char *InternW[] =
    {
    
    "string1", "string2", "string3", ...
    }
    but on rare occasions enumerating all strings gave me wrong output, so now I think this method is just as unsafe as
    Code:
    char buf[largenumber];
    I came up with a large list of realloc()ing the array buffer and appending the string to it - where I came into trouble with the pointers =/

    But is there a safe way of doing that? It's a standard console program, can it have resources, so the strings are saved as standalone binary data in the executable? I don't want to use a txt datafile because I want a sole exe.

    Thanks in advance, Hawk

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The way to get strings as standalone binary data in the executable is to do this:
    Code:
    const char *InternW[] =
    {
    
        "string1", "string2", "string3", ...
    
    }
    This gives you an array of pointers to where those strings live in memory. I don't know what "wrong output" you were getting; the usual culprit is trying to change these strings, which you can't.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    I see
    Will I be able to store millions of bytes without any problems this way?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Provided you have millions of bytes of RAM; most people do, these days. There's probably a limit on how large an array could be, but I would guess it's rather large; the size of the stack, divided by four or so, since the array is storing pointers and not the strings themselves (I can't find a listed minimum in the standard).

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    68
    Thanks, it seems to work just fine
    I'll post if I find the limit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 01:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 11:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 10:35 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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