473,394 Members | 1,748 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,394 software developers and data experts.

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 1931
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 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 <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...

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.