473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Inline initialization of a struct containing a string array



Hi!

Is it possible to inline initialize a struct whose one member is a string
array of arbitrary length (terminated w/ a NULL ptr)? What I mean is
something like this:
typedef struct {
char** x ;
int y ;
} Foo ;

static const Foo myfoos[] = {
{ { "hello", "world", NULL }, 12 },
{ NULL, 0 }
} ;
This produces warnings (from mingw gcc) for line w/ "hello" on it:

"initialization from incompatible pointer type"
"excess elements in scalar initializer"
"braces around scalar initializer"

So obviously this is not the way to do it, or I am missing something
here? Is
this possible at all?
It works fine if I define the first struct memeber as char* x[ 3 ] ; but
that
is not what I'm after - I need arbitrary length char* arrays.
As a last resort, I can do the initialization in the beginning of main
(i.e. just myfoos[0].x = ptrToFirstStrArray ; // ...), but that's not very
pretty...

.::Antti::.

Ps. I did consider "multistrings", e.g. "hello\0world\0", but that would
be inconsistent w/ the presentation of array of string values used in
other parts of the program.

Mar 27 '08 #1
2 8343
Antti Karanta wrote:
>

Hi!

Is it possible to inline initialize a struct whose one member is a string
array of arbitrary length (terminated w/ a NULL ptr)? What I mean is
something like this:
typedef struct {
char** x ;
int y ;
} Foo ;

static const Foo myfoos[] = {
{ { "hello", "world", NULL }, 12 },
{ NULL, 0 }
} ;
Define the arrays first, give them names, and use the
names when you initialize the structs.

static const char* fooArray0[] = {
"hello", "world", NULL };
static const char* fooArray1[] = {
"there", "is", "no", "Cabal", NULL };
static const Foo myfoos[] = {
{ fooArray0, 12 },
{ fooArray1, 42 },
{ NULL, 0 } };

It's not as slick as if you could somehow make the arrays
anonymous, but it works.

--
Er*********@sun.com
Mar 27 '08 #2
Antti Karanta wrote:
...
Is it possible to inline initialize a struct whose one member is a string
array of arbitrary length (terminated w/ a NULL ptr)? What I mean is
something like this:
typedef struct {
char** x ;
int y ;
} Foo ;

static const Foo myfoos[] = {
{ { "hello", "world", NULL }, 12 },
{ NULL, 0 }
} ;
...
You can do that in C99 by using compound literals

static const Foo myfoos[] = {
{ (char*[]) { "hello", "world", NULL }, 12 },
{ NULL, 0 }
};

But in C89/90 you'll have to resort to multiple declarations, I believe.

--
Best regards,
Andrey Tarasevich
Mar 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Timo | last post by:
I am trying to get address of myStruct to a string array texts. I am using M$ Visual C++ 6.0 (this is not OS specific question, though, this code should also work on 16 bit embedded compiler ;)). ...
3
by: Rudy Velthuis | last post by:
Hello, Does anyone know how to create a struct that will marshal to the following C++ struct A, containing an array of the user defined String10 type: struct String10 { char SLen; char S;
3
by: Thomas Connolly | last post by:
Hello list, No one seems to be replying to my request on the interop group so I thought I would try here. Can someone please help me with this? I have a C struct containing function pointers like...
5
by: Cybertof | last post by:
Hello, Is it possible to convert a VB6 Array of Struct to a C# Array Of Struct ? The test context is a C# application calling a VB6 ActiveX DLL Function using UDT (User Defined Type) and...
2
by: rmathieu | last post by:
Hi, I want to initialize a static String array in MC++. What I want to do is to initialize my String array like the C# way: new String {"11", "22"} but I could not find an equivalent in MC++. The...
7
by: Hendrik Schober | last post by:
Hi, I am trying to test a command line parser. So I came up with this (simplified): struct test { int argc; const char** argv; template< int N >
26
by: alberto | last post by:
Hi. Im newbie in C language. I have a binary file with many character arrays of 50 character defined as char array But in some cases, many of these 50 characters are not being used. I would...
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
12
by: JackYee123 | last post by:
Hey, I need a structure to store a string array in c, for example Index Content -------- ----------- 0 word1 1 word2 2 3
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.