how to define the array of strings

R

Roman Mashak

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 address removed)
 
R

Roman Mashak

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 address removed)
 
M

Martin Ambuhl

Roman said:
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;
 
J

john_bode

Roman said:
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 address 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");
}
 
E

Eric Sosman

[...]
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
 
M

Madhav

[...]
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"};
 
J

john_bode

Eric said:
[...]
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.
 
C

CBFalconer

Roman said:
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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top