help needed with structs 
Author Message
 help needed with structs

Can someone tell me how these things work?

struct StructName foo[] =
{
    [0] =
    {
        var1: "blah",
        var2: 1,
    },
    [1] =
    {
        var1: "bleh",
        var2: 2,
        var3: 0x04,
    },

Quote:
};

Namely, the "[n] ="s before every element and those "var1:", "var2:"s inside
the element declarations that shows the variables' names.  I recently saw
this in a Linux program and thought it was pretty cool, but when I tried it
in my own program, VC++ complained about both syntaxes.  How do they work?
Do they have a name?  Is this a C++ syntax?  What happens to var3 in element
0?  Aren't those trailing commas illegal?  Thanks!

[struct StructName is defined like an ordinary struct]

--
Atsushi Nakagawa

Unrelated messages will not be accepted by this mailbox.



Thu, 03 Feb 2005 01:39:47 GMT  
 help needed with structs

Quote:

> Can someone tell me how these things work?

> struct StructName foo[] =
> {
>     [0] =
>     {
>         var1: "blah",
>         var2: 1,
>     },
>     [1] =
>     {
>         var1: "bleh",
>         var2: 2,
>         var3: 0x04,
>     },
> };

> Namely, the "[n] ="s before every element and those "var1:", "var2:"s inside
> the element declarations that shows the variables' names.  I recently saw
> this in a Linux program and thought it was pretty cool, but when I tried it
> in my own program, VC++ complained about both syntaxes.

The `[index] =' notation is in C99, but the `member:' notation is
a GCC extension.  The C99 equivalent is `.member ='.  If VC++
doesn't support this syntax now, it will whenever it gets C99
support.

Quote:
> How do they work?

What do you mean?

Quote:
> Do they have a name?

They are called "designated initializers".

Quote:
> Is this a C++ syntax?

No.  This syntax is not available in C++98.

Quote:
> What happens to var3 in element
> 0?

It is initialized to the default value of 0.

Quote:
> Aren't those trailing commas illegal?

No.  Trailing commas are allowed in several places in C syntax
that have comma-separated lists.
--
"The fact that there is a holy war doesn't mean that one of the sides
 doesn't suck - usually both do..."
--Alexander Viro


Thu, 03 Feb 2005 01:46:19 GMT  
 help needed with structs


Quote:

> The `[index] =' notation is in C99, but the `member:' notation is
> a GCC extension.  The C99 equivalent is `.member ='.  If VC++
> doesn't support this syntax now, it will whenever it gets C99
> support.

> > How do they work?

> What do you mean?

I just wanted to know what you said above.  You've answered my enquiries
beautifully.  Thanks! :)

--
Atsushi Nakagawa

Unrelated messages will not be accepted by this mailbox.



Thu, 03 Feb 2005 10:22:55 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need DllImport expert to convert c++ to c# - same struct pointer in struct

2. desperate struct help needed

3. NEED HELP in Type casting structs to a char*'s to do comparisons

4. newbie needs help with struct

5. Aarghh! I need help with array of structs

6. Help needed with: sizeof (struct a)

7. I need *help* with Structs

8. need help in definition of a struct and an enum in ATL

9. Lists, Structs, need Help, etc.

10. Need help with XMS and struct. data type

11. Need help converting from tm struct to DATE

12. Need Help in C struct list

 

 
Powered by phpBB® Forum Software