array of struct init list

J

Jeff

Given a struct, TEST, I'd like to create a static end of array marker,
also of type test. This way static arrays of type TEST could be easily
created and their end could be marked with the end of array marker.

I think the code below is more clear than the explanation above:

//struct TEST
typedef struct
{
char *desc[2];
UINT mask;
}TEST;


/end of array marker
TEST END{"-1","-1",-1};


TEST t[] = {
{"a","b",1},
{"c","d",2},
END
};

This doesn't compile. 'initializing' : cannot convert from 'TEST' to
'char *'

I thought about using an array of pointers to TEST structs, and then
simply using NULL to mark the end of the array. But the static
initiliazation of such an array would be kind of a pain.

I'd have to initialize an array of TEST structs and then initalize the
array of pointers to struct w/ pointers to each element of the TEST
struct array.

TEST t[] = {
{"a","b",1},
{"c","d",2}
}

TEST *a[] = {&t[0],&t[1]};

Any ideas?

Thanks,
Jeff
 
R

Rob Adams

Jeff said:
Given a struct, TEST, I'd like to create a static end of array marker,
also of type test. This way static arrays of type TEST could be easily
created and their end could be marked with the end of array marker.

I think the code below is more clear than the explanation above:

//struct TEST
typedef struct
{
char *desc[2];
UINT mask;
}TEST;


/end of array marker
TEST END{"-1","-1",-1};


TEST t[] = {
{"a","b",1},
{"c","d",2},
END
};

Any ideas?

Hi, Jeff.

Two ideas come to mind. The first idea is very similar to yours:

#define TEST_END {"-1", "-1", -1}
TEST t[] = {
{ "a", "b", 1 },
{ "c", "d", 2 },
TEST_END
};

The second idea assumes that desc[0] must be non-null in all valid
entries. If that is the case, I'd prefer to do this:

TEST t[] = {
{ "a", "b", 1 },
{ "c", "d", 2 },
NULL
};

I suppose it is a matter of style whether to use NULL or TEST_END. I
find using NULL clearly expresses my intent, but others may prefer the
more explicit TEST_END macro.

I hope this helps,

Rob
 

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

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top