473,395 Members | 1,639 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,395 software developers and data experts.

array of struct init list

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
1 5470
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Grant Austin | last post by:
I'm implementing a hash table. I really think I'm doing everything acceptably but I'm still getting Seg Faults whenever I try and search the table and the symbol has not already been added to the...
2
by: kimimaro | last post by:
hi I wonder if array can be work along with structure? Below are the declaration of my structure struct employee{ char ID; char Name; char Department;
3
by: c8prog | last post by:
I want to save integer value in array(int key) of struct array (Element key; public int link; Element(int init) { init = 0; key = new int; link = init; } };
5
by: sherifffruitfly | last post by:
Hi, I'm just learning cpp, and the exercise I'm working on is basically as follows: 1) Create a struct type with 4 members (char, char, char, int). 2) Create an array of, say 3 instances of...
4
by: josh | last post by:
Hi, which is the way to init a static array inside a constructor function? i.e. if I had to init ---> int x = {100,200}
8
by: Francisco | last post by:
Hello, Is there any code faster than this array position manipulation (some code omitted for brevity)?: internal struct TreeNodeTableItem { public int a; public int b; public int c; public...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
6
by: npankey | last post by:
I've started experimenting with template metaprogramming in a small project of mine. What I'm trying to accomplish is to generate a static array of templated objects that get specialized based on...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.