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

Initialize a static array with struct elements

If I have a

struct S
{
int a;
float b;
};

how to initialize a static array of S?

static struct S sarray[] =
{
?????
};
Jun 27 '08 #1
3 12546

"syang8" <sy****@gmail.coma écrit dans le message de news:
be**********************************...oglegroups.com...
If I have a

struct S
{
int a;
float b;
};

how to initialize a static array of S?

static struct S sarray[] =
{
?????
};
Example
struct S
{
S() : a(0){} // ctor 1
explicit S(int i) : a(i){} // ctor 2
int a;
};

int main()
{
static S s1[2] = { S(1), S(2) }; // uses ctor 2
static S s2[2]; // uses ctor 1

return 0;
}

---------------

Eric Pruneau
Jun 27 '08 #2
syang8 wrote:
If I have a

struct S
{
int a;
float b;
};

how to initialize a static array of S?

static struct S sarray[] =
{
?????
};
What book are you reading that doesn't explain POD struct
initialization? Note that the "struct" in the definition
of sarray[] is a C-ism, not needed in C++.

static S sarray[] =
{
{ 1, 2.3 },
{ 4, 5.6 },
{ 7, 8.9 }
};
Jun 27 '08 #3
This answers my question. Thank you.

You are right. It is a pure C quesion. I think I posted on the wrong
group.

On Jun 20, 3:53*pm, red floyd <no.spam.h...@example.comwrote:
syang8 wrote:
If I have a
struct S
{
* int a;
* float b;
};
how to initialize a static array of S?
static struct S sarray[] =
{
* ?????
};

What book are you reading that doesn't explain POD struct
initialization? *Note that the "struct" in the definition
of sarray[] is a C-ism, not needed in C++.

static S sarray[] =
{
* * { 1, 2.3 },
* * { 4, 5.6 },
* * { 7, 8.9 }

};- Hide quoted text -

- Show quoted text -
Jun 27 '08 #4

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

Similar topics

4
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
2
by: slack_justyb | last post by:
Hello, I'm trying to figure the best way of doing the following. *I need an array of strings that are globally accessable from within 'arrayhere.h' *I need that array of strings to not change...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
15
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
10
by: Ni | last post by:
I found out smth strange in g++ >= 3.3.5 => I was able to do register char buff;//// where outSTDBuffer is a variable!!!! Is it a bug in gcc or specification of c++ changed?? I've noticed this...
10
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
1
by: Vol | last post by:
Hi, Group, I want to initialize a 2D static array in function 'foo ()', where 'foo' is called by another function a lot of times. I list my implementation below but I think there are better...
1
by: flowstudioLA | last post by:
I have a template class object that I use as a mesaging queue between threads. I use it as a static object that I initialize like so: foo.h class foo{ static LFQueue<const char*,100lfqMyQueue;...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.