Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Initialize pointer-to-struct declaration to an unnamed struct? (http://www.velocityreviews.com/forums/t517851-initialize-pointer-to-struct-declaration-to-an-unnamed-struct.html)

Ehud Shapira 06-26-2007 06:52 PM

Initialize pointer-to-struct declaration to an unnamed struct?
 
Is it possible to have a declaration of a struct pointer initialized
to an unnamed struct?
(I'm only concerned with static/global variables, if it matters.)

I'm trying to do something like:

struct st_a {
int i, j;
};

struct st_b {
int k, l;
st_a *m;
} b[] = {
1, 2, {0, 1},
3, 4, {0, 2}
};

It could be done by declaring each "sub-struct" with a name, then
pointing to these,
but besides being more work it's also more difficult to read when the
idea is a hierarchy.


Rolf Magnus 06-26-2007 08:12 PM

Re: Initialize pointer-to-struct declaration to an unnamed struct?
 
Ehud Shapira wrote:

> Is it possible to have a declaration of a struct pointer initialized
> to an unnamed struct?
> (I'm only concerned with static/global variables, if it matters.)
>
> I'm trying to do something like:
>
> struct st_a {
> int i, j;
> };
>
> struct st_b {
> int k, l;
> st_a *m;
> } b[] = {
> 1, 2, {0, 1},
> 3, 4, {0, 2}


{0, 1} is not a valid initializer for a pointer.

> };
>
> It could be done by declaring each "sub-struct" with a name, then
> pointing to these,
> but besides being more work it's also more difficult to read when the
> idea is a hierarchy.


It's not quite clear to me what you actually want.


Ehud Shapira 06-26-2007 08:47 PM

Re: Initialize pointer-to-struct declaration to an unnamed struct?
 
> {0, 1} is not a valid initializer for a pointer.
I know. What *can* be valid? You can see what sort of initialization
I'm trying to do.


Rolf Magnus 06-26-2007 09:17 PM

Re: Initialize pointer-to-struct declaration to an unnamed struct?
 
Ehud Shapira wrote:

>> {0, 1} is not a valid initializer for a pointer.

> I know. What *can* be valid? You can see what sort of initialization
> I'm trying to do.


Actually, I can't. Why does it have to be a pointer? Can't you just store a
struct directly?


Ehud Shapira 06-26-2007 09:52 PM

Re: Initialize pointer-to-struct declaration to an unnamed struct?
 
On Jun 26, 11:17 pm, Rolf Magnus <ramag...@t-online.de> wrote:
> Actually, I can't. Why does it have to be a pointer? Can't you just store a
> struct directly?

What I want to do involves a hierarchy of variable length struct
arrays. But regardless of the why, the question is whether it's
possible at all to do something like ( char *pc = "something" ) for a
struct and not char.


Victor Bazarov 06-26-2007 09:55 PM

Re: Initialize pointer-to-struct declaration to an unnamed struct?
 
Ehud Shapira wrote:
> On Jun 26, 11:17 pm, Rolf Magnus <ramag...@t-online.de> wrote:
>> Actually, I can't. Why does it have to be a pointer? Can't you just
>> store a struct directly?

> What I want to do involves a hierarchy of variable length struct
> arrays. But regardless of the why, the question is whether it's
> possible at all to do something like ( char *pc = "something" ) for a
> struct and not char.


Usually not. There is a huge difference between a string literal (that
has a very very very long lifetime) and a struct temporary. You could
create a static object of the struct type, and then declare a pointer
and initialise it with the address of the static object:

static somestruct something = { blah };
somestruct *ps = &something;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Ehud Shapira 06-26-2007 10:33 PM

Re: Initialize pointer-to-struct declaration to an unnamed struct?
 
>There is a huge difference between a string literal and a struct temporary.
The struct is just as static -- it's a global definition.

> You could create a static object of the struct type, and then declare a pointer
> and initialise it with the address of the static object:

That's what I'm trying to avoid; naming every linked array of structs.


Victor Bazarov 06-27-2007 12:33 AM

Re: Initialize pointer-to-struct declaration to an unnamed struct?
 
Ehud Shapira wrote:
>> There is a huge difference between a string literal and a struct
>> temporary.

> The struct is just as static -- it's a global definition.


No, a temporary created for the purposes of initialising something is
definitely not static. It's temporary.

>> You could create a static object of the struct type, and then
>> declare a pointer and initialise it with the address of the static
>> object:

> That's what I'm trying to avoid; naming every linked array of structs.


<shrug> Don't name them. Have an array and get pointers to its
elements:

struct st_a {
int i, j;
} a[] = { { 0, 1 }, {0, 2 } };

struct st_b {
int k, l;
st_a *m;
} b[] = {
{ 1, 2, a+0 },
{ 3, 4, a+1 }
};

You won't have "every" element named, only one.

Another solution is to create those dynamically:

struct st_a {
int i, j;
};

st_a* make_st_a(int i, j) {
st_a a = {i, j};
return new st_a(a);
}

struct st_b {
int k, l;
st_a *m;
} b[] = {
1, 2, make_st_a(0, 1),
3, 4, make_st_a(0, 2)
};

If 'b' is static, you don't have to care about disposing of
those dynamic objects, a small leak like that isn't going to
hurt much. Just be aware of it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



James Kanze 06-27-2007 10:03 AM

Re: Initialize pointer-to-struct declaration to an unnamed struct?
 
On Jun 26, 8:52 pm, Ehud Shapira <ehudshap...@hotmail.com> wrote:
> Is it possible to have a declaration of a struct pointer initialized
> to an unnamed struct?
> (I'm only concerned with static/global variables, if it matters.)


> I'm trying to do something like:


> struct st_a {
> int i, j;
> };


> struct st_b {
> int k, l;
> st_a *m;} b[] = {
> 1, 2, {0, 1},
> 3, 4, {0, 2}
> };


It can't be done in C++. In C you can write something like:

typedef struct
{
int i, j ;
} st_a ;

typedef struct
{
int k, l ;
st_a* m ;
} st_b ;

st_b b[] = {
{ 1, 2, &(st_a){ 0, 1 }, },
{ 3, 4, &(st_a){ 0, 2 }, },
} ;

but this was added to C after C++ was standardized. (Possibly
some C++ compilers support it anyway. I don't know.)

> It could be done by declaring each "sub-struct" with a name, then
> pointing to these,
> but besides being more work it's also more difficult to read when the
> idea is a hierarchy.


In such cases, I usually define the data in a simple text file,
and then write a small program to convert it into legal C++.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34



ehudshapira@gmail.com 06-27-2007 03:19 PM

Re: Initialize pointer-to-struct declaration to an unnamed struct?
 
On Jun 27, 2:33 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> The struct is just as static -- it's a global definition.

> No, a temporary created for the purposes of initialising something is
> definitely not static. It's temporary.


I don't understand that. Maybe we are talking about different things?
The struct will be initialized at compile-time, and so will live
"forever". How is that temporary?

> <shrug> Don't name them. Have an array and get pointers to its
> elements:


Bookkeeping array indexes isn't much better. And dynamically creating
them would add overhead, etc.

On Jun 27, 12:03 pm, James Kanze <james.ka...@gmail.com> wrote:
> In such cases, I usually define the data in a simple text file,
> and then write a small program to convert it into legal C++.


Yeah, if that's the case, for big definitions I'll probably go with a
custom preprocessor, possibly outputting binary directly (which would
also rid me of unclear handling of union initialization by VC6... but
maybe that's the problem; using an old compiler.)

Too bad C/++ doesn't offer (in many cases) such terseness as that
possible by, say, JavaScript.

Thanks all for the feedback.



All times are GMT. The time now is 05:45 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.