469,275 Members | 1,463 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Array of structures

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.

Jul 22 '05 #1
8 1729
michi wrote:

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());

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #2
Andrey Tarasevich wrote:

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.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #3
Andrey Tarasevich wrote:
...
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).

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #4
Andrey Tarasevich <an**************@hotmail.com> wrote in
news:10*************@news.supernews.com:
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

Jul 22 '05 #5
"michi" <mi***@fossilgroup.net> wrote in message
news:Xn*********************************@195.34.13 2.67...
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]();
Jul 22 '05 #6
Andrew Koenig wrote:
"michi" <mi***@fossilgroup.net> wrote in message
news:Xn*********************************@195.34.13 2.67...

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
Jul 22 '05 #7
> 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?
you can simply provide a constructor for it

Jul 22 '05 #8
Method Man wrote:
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 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'.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

104 posts views Thread by Leszek | last post: by
7 posts views Thread by Sam | last post: by
44 posts views Thread by svata | last post: by
5 posts views Thread by =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.