9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] initialization of anonymous structs.
@ 2008-12-04 12:07 Gorka Guardiola
  2008-12-04 12:26 ` Gorka Guardiola
  0 siblings, 1 reply; 5+ messages in thread
From: Gorka Guardiola @ 2008-12-04 12:07 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Say I have a couple of structs like:

typedef struct A A;
typedef struct B B;

struct A
{
      int a1;
      int a2;
};

struct B
{
     A;
     int b1;
     int b2;
};

Now I want to declare a variable of kind B with parts initialized. Is
there anyway to initialize the A inside the B?. I have tried:

B bvar = {
    .a1 2
    .b1 2
};


B bvar = {
    .a1=2
    .b1=2
};

B bvar = {
    .A { .a1=2}
    .b1 2
};

B bvar = {
    .A={ .a1 2}
    .b1 2
};

B bvar = {
    .A={2}
    .b1 2
};

I always get the error that the field does not exist.

I have looked at the code of the compiler
and run it with -di and have not been able to figure out a way to initialize
the anonymous struct.

Am I missing something?. Is there a way to do this?.

--
- curiosity sKilled the cat



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] initialization of anonymous structs.
  2008-12-04 12:07 [9fans] initialization of anonymous structs Gorka Guardiola
@ 2008-12-04 12:26 ` Gorka Guardiola
  2008-12-04 12:33   ` Sape Mullender
  0 siblings, 1 reply; 5+ messages in thread
From: Gorka Guardiola @ 2008-12-04 12:26 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On Thu, Dec 4, 2008 at 1:07 PM, Gorka Guardiola <paurea@gmail.com> wrote:
> Say I have a couple of structs like:
>
> typedef struct A A;
> typedef struct B B;
>
> struct A
> {
>      int a1;
>      int a2;
> };
>
> struct B
> {
>     A;
>     int b1;
>     int b2;
> };
>
> Now I want to declare a variable of kind B with parts initialized. Is
> there anyway to initialize the A inside the B?. I have tried:
>
> B bvar = {
>    .a1 2
>    .b1 2
> };
>

There is a way, with a display, but I wanted to name the fields.
This works:

B bvar = {
  0,
  0,
  0,
  0
};

I guess I can always add comments.

--
- curiosity sKilled the cat



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] initialization of anonymous structs.
  2008-12-04 12:26 ` Gorka Guardiola
@ 2008-12-04 12:33   ` Sape Mullender
  2008-12-04 14:56     ` roger peppe
  0 siblings, 1 reply; 5+ messages in thread
From: Sape Mullender @ 2008-12-04 12:33 UTC (permalink / raw)
  To: 9fans

[-- Attachment #1: Type: text/plain, Size: 198 bytes --]

typedef struct A A;
typedef struct B B;

struct A
{
     int a1;
     int a2;
};

struct B
{
    A;
    int b1;
    int b2;
};

B bvar = {
	.B = {
		.a1 = 2,
		.b1 = 2,
	},
};

[-- Attachment #2: Type: message/rfc822, Size: 2716 bytes --]

From: "Gorka Guardiola" <paurea@gmail.com>
To: "Fans of the OS Plan 9 from Bell Labs" <9fans@9fans.net>
Subject: Re: [9fans] initialization of anonymous structs.
Date: Thu, 4 Dec 2008 13:26:55 +0100
Message-ID: <599f06db0812040426u435f5a9dx7c1c0209c333db53@mail.gmail.com>

On Thu, Dec 4, 2008 at 1:07 PM, Gorka Guardiola <paurea@gmail.com> wrote:
> Say I have a couple of structs like:
>
> typedef struct A A;
> typedef struct B B;
>
> struct A
> {
>      int a1;
>      int a2;
> };
>
> struct B
> {
>     A;
>     int b1;
>     int b2;
> };
>
> Now I want to declare a variable of kind B with parts initialized. Is
> there anyway to initialize the A inside the B?. I have tried:
>
> B bvar = {
>    .a1 2
>    .b1 2
> };
>

There is a way, with a display, but I wanted to name the fields.
This works:

B bvar = {
  0,
  0,
  0,
  0
};

I guess I can always add comments.

--
- curiosity sKilled the cat

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] initialization of anonymous structs.
  2008-12-04 12:33   ` Sape Mullender
@ 2008-12-04 14:56     ` roger peppe
  2008-12-04 15:16       ` erik quanstrom
  0 siblings, 1 reply; 5+ messages in thread
From: roger peppe @ 2008-12-04 14:56 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

On Thu, Dec 4, 2008 at 12:33 PM, Sape Mullender
<sape@plan9.bell-labs.com> wrote:
> B bvar = {
>        .B = {
>                .a1 = 2,
>                .b1 = 2,
>        },
> };

that doesn't work for me.
i get:
x.c:18 structure element not found B
x.c:18 more initializers than structure: bvar

changing the .B to .A doesn't work either, nor does
{.A = {.a1 = 3}, .b1 = 4}
which intuitively (without looking at the compiler code) seems the
most likely to work...



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] initialization of anonymous structs.
  2008-12-04 14:56     ` roger peppe
@ 2008-12-04 15:16       ` erik quanstrom
  0 siblings, 0 replies; 5+ messages in thread
From: erik quanstrom @ 2008-12-04 15:16 UTC (permalink / raw)
  To: 9fans

> On Thu, Dec 4, 2008 at 12:33 PM, Sape Mullender
> <sape@plan9.bell-labs.com> wrote:
>> B bvar = {
>>        .B = {
>>                .a1 = 2,
>>                .b1 = 2,
>>        },
>> };
>
> that doesn't work for me.
> i get:
> x.c:18 structure element not found B
> x.c:18 more initializers than structure: bvar
>
> changing the .B to .A doesn't work either, nor does
> {.A = {.a1 = 3}, .b1 = 4}
> which intuitively (without looking at the compiler code) seems the
> most likely to work...

seems like a subtle compiler nit (i hope i'm not out-of-date
again), this initialization does work

B bvar = {
	{.a1 = 1,},
	.b1 = 1,
};

the {} fix the problem but should not be necessary.

- erik




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-12-04 15:16 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-04 12:07 [9fans] initialization of anonymous structs Gorka Guardiola
2008-12-04 12:26 ` Gorka Guardiola
2008-12-04 12:33   ` Sape Mullender
2008-12-04 14:56     ` roger peppe
2008-12-04 15:16       ` erik quanstrom

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).