473,229 Members | 1,806 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,229 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 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?


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);"...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...

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.