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. 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
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
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
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
"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]();
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
> 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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:...
|
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...
|
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...
|
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...
|
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;
|
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;
|
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...
|
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);"...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
| |