Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to define the array of strings

Reply
Thread Tools

how to define the array of strings

 
 
Roman Mashak
Guest
Posts: n/a
 
      06-13-2005
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: http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Roman Mashak
Guest
Posts: n/a
 
      06-13-2005
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: (E-Mail Removed)


 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-13-2005
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;

 
Reply With Quote
 
john_bode@my-deja.com
Guest
Posts: n/a
 
      06-13-2005


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: (E-Mail Removed)


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");
}

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-13-2005
(E-Mail Removed) 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
(E-Mail Removed)lid
 
Reply With Quote
 
Madhav
Guest
Posts: n/a
 
      06-13-2005
> (E-Mail Removed) 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"};

 
Reply With Quote
 
john_bode@my-deja.com
Guest
Posts: n/a
 
      06-13-2005


Eric Sosman wrote:
> (E-Mail Removed) 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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-13-2005
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[i]);

--
"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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: Preprocessor automation to define/declare array of strings Eric Sosman C Programming 9 09-07-2012 08:49 PM
Re: Preprocessor automation to define/declare array of strings Jorgen Grahn C Programming 1 08-11-2012 05:31 PM
Re: Preprocessor automation to define/declare array of strings Ike Naar C Programming 0 08-06-2012 02:55 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM



Advertisments