473,229 Members | 1,806 Online

# 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 1923
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?

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?

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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 4 by: emma middlebrook | last post by: Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving... 8 by: ulyses | last post by: I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:... 104 by: Leszek | last post by: Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from... 7 by: Sam | last post by: Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change... 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... 11 by: skumar434 | last post by: Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id; 44 by: svata | last post by: Hello, I wonder how to resize such array of structures using realloc()? #include #include #define FIRST 7 typedef struct { char *name; 17 by: Ben Bacarisse | last post by: candide