473,769 Members | 3,828 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_(fa lse){}
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 2952
Hi

"Simon Elliott" <Simon at ctsn.co.uk> wrote:
struct bar
{
bar(void):i1_(0 ),i2_(0),b1_(fa lse){}
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_(fa lse){}
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
5436
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. I know if I change char name this will work, but I will waste alot of space. (I am learning so I want to learn the best way) How can I define a struct the allows variable length character strings in the
5
2072
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 * relAttrs; /* definition shown at end of post */ };
10
4126
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
2972
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) struct, I am storing an array of 27 pointers and a void pointer that can point to anything. typedef struct trieNode { struct trieNode *children; // The children nodes void *obj; // The object stored } TrieNode;
15
5323
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 private string myArray;
2
7211
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
10167
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". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
12
3885
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
2
5216
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 myClass(); myInstance.member_0 = memberValue_0; // absolute notation myInstance = memberValue_0; // relative notation myInstance = memberValue_0; // nominal notation You can initialize a struct in C/C++ like you would an array, as with the...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9851
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8863
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.