472,780 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 8306
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.