Array of structures

M

michi

Hello everybody,

I have following problem: I have an array of pointers to structures:

table* tab = new table[10];

and structure table is like this:

struct table{
CSLL::node* chain;
};

so in this structure, there is one pointer. And I want to set all these pointers to 0 when I create the
array.
Yes, I can do that by:

for(int i=0;i<10;i++){
tab[a].chain = 0;
},

but I think I can do it better way. Any suggestions?

Thanks, michi.
 
A

Andrey Tarasevich

michi said:
I have following problem: I have an array of pointers to structures:

table* tab = new table[10];

That's array of structures, not array of pointers to structures.
and structure table is like this:

struct table{
CSLL::node* chain;
};

so in this structure, there is one pointer. And I want to set all these pointers to 0 when I create the
array.
Yes, I can do that by:

for(int i=0;i<10;i++){
tab[a].chain = 0;
},

but I think I can do it better way. Any suggestions?

Well, that's not that bad. BTW, what exactly do you mean by "better"?
You can do it like this

for (table *it = tab, *it_end = it + 10; it != it_end; ++it)
it->chain = NULL;

Does that qualify as better? Or maybe

std::fill(tab, tab + 10, table());

or

std::fill_n(tab, 10, table());
 
A

Andrey Tarasevich

Andrey said:
I have following problem: I have an array of pointers to structures:

table* tab = new table[10];

That's array of structures, not array of pointers to structures.
and structure table is like this:

struct table{
CSLL::node* chain;
};

so in this structure, there is one pointer. And I want to set all these pointers to 0 when I create the
array.
Yes, I can do that by:

for(int i=0;i<10;i++){
tab[a].chain = 0;
},

but I think I can do it better way. Any suggestions?

Well, that's not that bad. BTW, what exactly do you mean by "better"?
You can do it like this

for (table *it = tab, *it_end = it + 10; it != it_end; ++it)
it->chain = NULL;

Does that qualify as better? Or maybe

std::fill(tab, tab + 10, table());

or

std::fill_n(tab, 10, table());

Of course, if you don't mind losing the "aggregate-ness" of your struct,
you can simply provide a constructor for it

struct table{
CSLL::node* chain;

table() : chain()
{}
};

and forget about the cycle completely.
 
M

michi

Andrey Tarasevich wrote:
Of course, if you don't mind losing the "aggregate-ness" of your
struct, you can simply provide a constructor for it

struct table{
CSLL::node* chain;

table() : chain()
{}
};

and forget about the cycle completely.

yees, this is exactly what I wanted, I didn't know how to use constructor for a structure.
Thanks.
michi
 
A

Andrey Tarasevich

Andrey said:
...
I have following problem: I have an array of pointers to structures:

table* tab = new table[10];

That's array of structures, not array of pointers to structures.
and structure table is like this:

struct table{
CSLL::node* chain;
};

so in this structure, there is one pointer. And I want to set all these pointers to 0 when I create the
array.
Yes, I can do that by:

for(int i=0;i<10;i++){
tab[a].chain = 0;
},

but I think I can do it better way. Any suggestions?

Well, that's not that bad. BTW, what exactly do you mean by "better"?
You can do it like this

for (table *it = tab, *it_end = it + 10; it != it_end; ++it)
it->chain = NULL;

Does that qualify as better? Or maybe

std::fill(tab, tab + 10, table());

or

std::fill_n(tab, 10, table());

Of course, if you don't mind losing the "aggregate-ness" of your struct,
you can simply provide a constructor for it

struct table{
CSLL::node* chain;

table() : chain()
{}
};

and forget about the cycle completely.
...

And, finally, (sorry for the triple post) you can just switch to using
'std::vector' instead of plain array. Just do

std::vector<table> tbl(10);

and you get an array of 10 'table's, all nicely initialized with
null-pointer values in 'chain' field (even if the struct has no
user-defined constructor).
 
A

Andrew Koenig

I have following problem: I have an array of pointers to structures:

table* tab = new table[10];

That's not an array of pointers, it's a pointer to the initial element of an
array.
and structure table is like this:

struct table{
CSLL::node* chain;
};

so in this structure, there is one pointer. And I want to set all these
pointers to 0 when I create the
array.
Yes, I can do that by:

for(int i=0;i<10;i++){
tab[a].chain = 0;
},

but I think I can do it better way. Any suggestions?

How about this?

table* tab = new table[10]();
 
V

Victor Bazarov

Andrew said:
I have following problem: I have an array of pointers to structures:

table* tab = new table[10];


That's not an array of pointers, it's a pointer to the initial element of an
array.

and structure table is like this:

struct table{
CSLL::node* chain;
};

so in this structure, there is one pointer. And I want to set all these
pointers to 0 when I create the
array.
Yes, I can do that by:

for(int i=0;i<10;i++){
tab[a].chain = 0;
},

but I think I can do it better way. Any suggestions?


How about this?

table* tab = new table[10]();

Just a warning: Visual C++ v6 unfortunately has a bug, it doesn't
default-initialise the array in this case. Watch out for other
compilers too doing it incorrectly... VC++ v7.1 has it fixed.

V
 
M

Method Man

Of course, if you don't mind losing the "aggregate-ness" of your struct,

Can you please explain what you mean by the above line?
 
A

Andrey Tarasevich

Method said:
Can you please explain what you mean by the above line?
...

A structure defined as

struct table
{
CSLL::node* chain;
};

is an aggregate (see definition in 8.5.1/1) and can be initialized with
an aggregate initializer (initializer enclosed in '{}' ). For example

table tab1 = { NULL };
table tab2 = {};
table tab3 = { tab1.chain };
// etc...

Once the user-defined constructor is introduced, the structure is no
longer an aggregate and aggregate initializer can no longer be used with
objects of type 'table'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top