www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Creating an array of C structs

reply "Colin Grogan" <grogan.colin gmail.com> writes:
Why wont the following code compile?

import std.stdio;

void main()
{
     myStruct[] mystructs = {
         {1, 1.1f},
         {2, 2.2f}
     };
}

extern(C){
     struct myStruct{
         int x;
         float y;
     }
}

It fails with the (unhelpful imo) error message:
source/app.d(7): Error: a struct is not a valid initializer for a 
myStruct[]

The reason I need a C struct is because I'm interfacing to a C 
library that expects an array of myStructs.
Jan 27 2014
parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Monday, 27 January 2014 at 09:06:17 UTC, Colin Grogan wrote:
 Why wont the following code compile?

 import std.stdio;

 void main()
 {
     myStruct[] mystructs = {
         {1, 1.1f},
         {2, 2.2f}
     };
 }

 extern(C){
     struct myStruct{
         int x;
         float y;
     }
 }

 It fails with the (unhelpful imo) error message:
 source/app.d(7): Error: a struct is not a valid initializer for 
 a myStruct[]

 The reason I need a C struct is because I'm interfacing to a C 
 library that expects an array of myStructs.
myStruct[] mystructs = [ {1, 1.1f}, {2, 2.2f} ]; Arrays are enclosed in [] ;)
Jan 27 2014
parent reply "Colin Grogan" <grogan.colin gmail.com> writes:
On Monday, 27 January 2014 at 09:34:04 UTC, Namespace wrote:
 On Monday, 27 January 2014 at 09:06:17 UTC, Colin Grogan wrote:
 Why wont the following code compile?

 import std.stdio;

 void main()
 {
    myStruct[] mystructs = {
        {1, 1.1f},
        {2, 2.2f}
    };
 }

 extern(C){
    struct myStruct{
        int x;
        float y;
    }
 }

 It fails with the (unhelpful imo) error message:
 source/app.d(7): Error: a struct is not a valid initializer 
 for a myStruct[]

 The reason I need a C struct is because I'm interfacing to a C 
 library that expects an array of myStructs.
myStruct[] mystructs = [ {1, 1.1f}, {2, 2.2f} ]; Arrays are enclosed in [] ;)
I'm an idiot. Can I delete this thread to save further embarrassment? :)
Jan 27 2014
next sibling parent "Stanislav Blinov" <stanislav.blinov gmail.com> writes:
On Monday, 27 January 2014 at 10:13:08 UTC, Colin Grogan wrote:
 Arrays are enclosed in [] ;)
I'm an idiot. Can I delete this thread to save further embarrassment? :)
No! Evenryone will see this! >:E~
Jan 27 2014
prev sibling parent reply "Francesco Cattoglio" <francesco.cattoglio gmail.com> writes:
On Monday, 27 January 2014 at 10:13:08 UTC, Colin Grogan wrote:
 On Monday, 27 January 2014 at 09:34:04 UTC, Namespace wrote:
 Arrays are enclosed in [] ;)
I'm an idiot. Can I delete this thread to save further embarrassment? :)
HA-HA! (read it with Nelson voice, ofc)
Jan 27 2014
parent reply "Colin Grogan" <grogan.colin gmail.com> writes:
On Monday, 27 January 2014 at 10:39:28 UTC, Francesco Cattoglio 
wrote:
 On Monday, 27 January 2014 at 10:13:08 UTC, Colin Grogan wrote:
 On Monday, 27 January 2014 at 09:34:04 UTC, Namespace wrote:
 Arrays are enclosed in [] ;)
I'm an idiot. Can I delete this thread to save further embarrassment? :)
HA-HA! (read it with Nelson voice, ofc)
In my defense, I believe C initializes arrays with the curly brackets.... Can I keep making excuses?
Jan 27 2014
parent "Francesco Cattoglio" <francesco.cattoglio gmail.com> writes:
On Monday, 27 January 2014 at 13:08:28 UTC, Colin Grogan wrote:

 In my defense, I believe C initializes arrays with the curly 
 brackets....
 Can I keep making excuses?
Yes you can... And don't worry, I mess up initialization too. Lots of time. Especially when I tried initializing an array of structs made of 2 structs. I think I wasted at least 15 minutes before giving up :D
Jan 27 2014