how to define the array of strings

Discussion in 'C Programming' started by Roman Mashak, Jun 13, 2005.

  1. Roman Mashak

    Roman Mashak Guest

    Hello, All!

    I wonder is it possible to define an array containing strings, not
    single characters? What I want is array 'table[N]' that will have N
    elements, and every element is a strings tailoring with '\0', like that:

    table[0] = "string0\0"
    table[1] = "string1\0"
    ...

    Unfortunately I have no idea how to do this... May be declare new type
    and then declare my array of this new type?

    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Jun 13, 2005
    #1
    1. Advertising

  2. Roman Mashak

    Roman Mashak Guest

    Hello, Roman!
    You wrote to All on Mon, 13 Jun 2005 14:31:50 +0900:

    RM> I wonder is it possible to define an array containing strings, not
    RM> single characters? What I want is array 'table[N]' that will have N
    RM> elements, and every element is a strings tailoring with '\0', like
    RM> that:

    RM> table[0] = "string0\0"
    RM> table[1] = "string1\0"
    RM> ...

    RM> Unfortunately I have no idea how to do this... May be declare new
    RM> type and then declare my array of this new type?
    Please cancel post. I was overthinking the problem :)

    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Jun 13, 2005
    #2
    1. Advertising

  3. Roman Mashak wrote:
    > Hello, All!
    >
    > I wonder is it possible to define an array containing strings, not
    > single characters? What I want is array 'table[N]' that will have N
    > elements, and every element is a strings tailoring with '\0', like that:
    >
    > table[0] = "string0\0"
    > table[1] = "string1\0"


    char *table[] = {
    "string0",
    "string1"
    };
    size_t N = sizeof table / sizeof *table;
     
    Martin Ambuhl, Jun 13, 2005
    #3
  4. Roman Mashak

    Guest

    Roman Mashak wrote:
    > Hello, All!
    >
    > I wonder is it possible to define an array containing strings, not
    > single characters? What I want is array 'table[N]' that will have N
    > elements, and every element is a strings tailoring with '\0', like that:
    >
    > table[0] = "string0\0"
    > table[1] = "string1\0"
    > ...
    >
    > Unfortunately I have no idea how to do this... May be declare new type
    > and then declare my array of this new type?
    >
    > With best regards, Roman Mashak. E-mail:


    There are several ways. Remember that in C a string is a
    zero-terminated array of char, so one approach is to define a
    two-dimensional array of char, with the second dimension long enough to
    hold your longest string:

    char table[5][10] = {"foo", "ten chars", "blurga", "a string", "S"};

    The main drawback to this approach is the potential for lots of wasted
    space. If you have 20 strings, 19 of which are 10 characters long
    (including terminator), and 1 string that's 50 characters long
    (including terminator), then you're wasting 40*19 bytes of storage.

    You can elide the second dimension, however, and the array will be
    sized for the initializer:

    char table[5][] = {"foo", "ten chars", "blurga", "a string", "S"};

    So sizeof table[0] == 4, sizeof table[1] == 10, sizeof table[2] == 7,
    etc. No wasted space. On the other hand, if you want to change the
    contents of each entry, you have to stick with the initial array size
    (in other words, table[0] can hold at most 4 characters including the
    terminator, whereas table[1] can hold up to 10).

    Another approach is to define an array of pointers to char, and set
    each element to point to a string literal:

    char *table[5] = {"foo", "ten chars", "blurga", "a string", "S"};

    However, since each array element points to a string literal, you
    cannot modify the contents of each element; you can, however, reassign
    the pointer (IOW strcpy(table[0], "bar"); won't work, but table[0] =
    "bar" will).

    The last way is to define an array of pointers to char, and then use
    malloc() (or calloc()) to allocate each string:

    char *table[5];

    table[0] = malloc(strlen("foo") + 1);
    strcpy(table[0], "foo");

    table[1] = malloc(strlen("ten chars") + 1);
    strcpy(table[1], "ten chars");

    etc.

    This approach offers the most advantages: no wasted space, you can
    modify the contents of each element, and you can resize each element as
    necessary using realloc() if you need to write a longer string to that
    entry:

    char *tmp;
    tmp = realloc(table[0], strlen("a much, much longer string than the
    original") + 1);
    if (tmp)
    {
    table[0] = tmp;
    strcpy(table[0], "a much, much longer string than the original");
    }
     
    , Jun 13, 2005
    #4
  5. Roman Mashak

    Eric Sosman Guest

    wrote:
    > [...]
    > You can elide the second dimension, however, and the array will be
    > sized for the initializer:
    >
    > char table[5][] = {"foo", "ten chars", "blurga", "a string", "S"};


    Er, no: that's not C. Here's what gcc thinks of it:

    foo.c:1: warning: array type has incomplete element type
    foo.c:1: error: elements of array `table' have incomplete type
    foo.c:1: error: storage size of `table' isn't known

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 13, 2005
    #5
  6. Roman Mashak

    Madhav Guest

    > wrote:
    > > [...]
    > > You can elide the second dimension, however, and the array will be
    > > sized for the initializer:
    > >
    > > char table[5][] = {"foo", "ten chars", "blurga", "a string", "S"};

    >
    > Er, no: that's not C. Here's what gcc thinks of it:
    >


    Right...
    It could be done like this:
    char table[][10] = {"foo", "ten chars", "blurga", "a string", "S"};
     
    Madhav, Jun 13, 2005
    #6
  7. Roman Mashak

    Guest

    Eric Sosman wrote:
    > wrote:
    > > [...]
    > > You can elide the second dimension, however, and the array will be
    > > sized for the initializer:
    > >
    > > char table[5][] = {"foo", "ten chars", "blurga", "a string", "S"};

    >
    > Er, no: that's not C. Here's what gcc thinks of it:
    >


    Gah, you're right. Serves me right for trying to answer a question
    before I'm actually awake. Don't know what I was thinking.
     
    , Jun 13, 2005
    #7
  8. Roman Mashak

    CBFalconer Guest

    Roman Mashak wrote:
    >
    > I wonder is it possible to define an array containing strings, not
    > single characters? What I want is array 'table[N]' that will have
    > N elements, and every element is a strings tailoring with '\0',
    > like that:
    >
    > table[0] = "string0\0"
    > table[1] = "string1\0"
    > ...
    >
    > Unfortunately I have no idea how to do this... May be declare new
    > type and then declare my array of this new type?


    const char *table[] = "string1", "string2", "string3";

    for (i = 0; i < (sizeof table / sizeof table[0]); i++)
    puts(table);

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Jun 13, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. theotyflos
    Replies:
    3
    Views:
    473
    Thomas Matthews
    Feb 19, 2004
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
  3. Eric Sosman
    Replies:
    9
    Views:
    648
    Tim Rentsch
    Sep 7, 2012
  4. Ike Naar
    Replies:
    0
    Views:
    344
    Ike Naar
    Aug 6, 2012
  5. Jorgen Grahn
    Replies:
    1
    Views:
    348
    Stefan Ram
    Aug 11, 2012
Loading...

Share This Page