473,396 Members | 2,081 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,396 software developers and data experts.

Initialise array of struct using C syntax

I have some legacy code which initialises an array using C syntax like
this:

struct bar
{
int i1_;
int i2_;
bool b1_;
};

int main(int argc, char **argv)
{
bar myBar[] =
{
{1,2,false},
{2,2,true},
};
return 0;
}

I now want to use struct bar in C++ and give it a constructor or two:

struct bar
{
bar(void):i1_(0),i2_(0),b1_(false){}
bar(int i1, int i2, bool b1):i1_(i1),i2_(i2),b1_(b1){}
int i1_;
int i2_;
bool b1_;
};

Is there any way I can still use the above syntax for initialising an
array of bar?

--
Simon Elliott http://www.ctsn.co.uk
Jul 23 '05 #1
2 2936
Hi

"Simon Elliott" <Simon at ctsn.co.uk> wrote:
struct bar
{
bar(void):i1_(0),i2_(0),b1_(false){}
bar(int i1, int i2, bool b1):i1_(i1),i2_(i2),b1_(b1){}
int i1_;
int i2_;
bool b1_;
};

Is there any way I can still use the above syntax for initialising an
array of bar?


No, but what about:

bar array[] = {
bar(1,2,true),
bar(3,4,false),
bar(5,0,true),
bar()
};

Markus

Jul 23 '05 #2
On 13/04/2005, Markus Moll wrote:
"Simon Elliott" <Simon at ctsn.co.uk> wrote:
struct bar
{
bar(void):i1_(0),i2_(0),b1_(false){}
bar(int i1, int i2, bool b1):i1_(i1),i2_(i2),b1_(b1){}
int i1_;
int i2_;
bool b1_;
};

Is there any way I can still use the above syntax for initialising
an array of bar?


No, but what about:

bar array[] = {
bar(1,2,true),
bar(3,4,false),
bar(5,0,true),
bar()
};


Thanks, that's exactly what I was looking for.
--
Simon Elliott http://www.ctsn.co.uk
Jul 23 '05 #3

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

Similar topics

6
by: Stuart Norris | last post by:
Dear Readers, I am attempting to initialise a struct contiaing a dynamic character string. In the example below I am trying to initialise the name field so that my struct does not waste space. ...
5
by: Grant Austin | last post by:
What would be the correct syntax for setting up a dynamic array of structs? Suppose you have a struct declared: struct relation { FILE * binFile; unsigned int numAttrs; struct attrList *...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
2
by: Charles Law | last post by:
Does anyone know if it is possible to initialise a structure array at run-time, something like this: <code> Structure struct Dim a As String Dim b As Integer Dim c As End Structure
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
2
by: berrylthird | last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example: var myInstance:myClass = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.