By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

array of struct init list

P: n/a
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

Dec 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Jeff wrote:
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
Dec 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.