list of strings

T

todorov

Hi all,

I have a strange problem occuring when i declare an array of strings in
ANSI C style:

if i have something like :

struct blabla {

//...
char *names[] = {"name1","name2", "name3" };

};

, it doesn't work. But if i declare it by itself (not in a struct - i.e
for instance in the outmost scope of any function) , it works. What am
i doing wrong? Thanks a lot.
 
K

Keith Thompson

todorov said:
I have a strange problem occuring when i declare an array of strings in
ANSI C style:

if i have something like :

struct blabla {

//...
char *names[] = {"name1","name2", "name3" };

};

, it doesn't work. But if i declare it by itself (not in a struct - i.e
for instance in the outmost scope of any function) , it works. What am
i doing wrong? Thanks a lot.

A struct member declaration can't include an initializer.

You can initialize a struct member as part of the initializer for a
struct object.
 
M

Mike Wahler

todorov said:
Hi all,

I have a strange problem occuring when i declare an array of strings in
ANSI C style:

if i have something like :

struct blabla {

//...
char *names[] = {"name1","name2", "name3" };

};

, it doesn't work. But if i declare it by itself (not in a struct - i.e
for instance in the outmost scope of any function) , it works. What am
i doing wrong? Thanks a lot.

struct blabla {
char *names[3];
} bla = {{"name1", "name2", "name3"}};

-Mike
 
E

Emmanuel Delahaye

todorov a écrit :
struct blabla {

//...
char *names[] = {"name1","name2", "name3" };

};

Wrong way. Try this :

struct blabla {
//...
char *names[] = {"name1","name2", "name3" };
}
=
{
//... ,
{"name1", "name2", "name3"},
};
 
E

Emmanuel Delahaye

todorov a écrit :
struct blabla {

//...
char *names[] = {"name1","name2", "name3" };

};

Wrong way. Try this :

struct blabla {
//...
char **names;
}
=
{
//... ,
{"name1", "name2", "name3"},
};
 
S

Simon Biber

Emmanuel said:
todorov a écrit :
struct blabla {

//...
char *names[] = {"name1","name2", "name3" };

};


Wrong way. Try this :

struct blabla {
//...
char **names;

You can't initialise a scalar with more than one element. This should be
char *names[3];

The name of the object you are declaring is missing.
 
B

Barry Schwarz

Hi all,

I have a strange problem occuring when i declare an array of strings in
ANSI C style:

if i have something like :

struct blabla {

//...
char *names[] = {"name1","name2", "name3" };

};

, it doesn't work. But if i declare it by itself (not in a struct - i.e
for instance in the outmost scope of any function) , it works. What am
i doing wrong? Thanks a lot.

That is not an array of strings. It is an array of pointers.


<<Remove the del for email>>
 

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
474,172
Messages
2,570,934
Members
47,473
Latest member
ChristelPe

Latest Threads

Top