954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array in struct

I have this C++ code :

const int ANYSIZE_ARRAY = 1;
    struct LUID {DWORD LowPart; WORD HighPart;};
    struct LUID_AND_ATTRIBUTES {LUID Luid; DWORD Attributes;};
    struct TOKEN_PRIVILEGES {DWORD PrivilegeCount;   LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];};


I want to convert this code into C# syntax.

I tried this one, but don't know how to place array in a struct.

const int ANYSIZE_ARRAY = 1;
    struct LUID {Int32 LowPart; Int16 HighPart;};
    struct LUID_AND_ATTRIBUTES {LUID Luid; Int32 Attributes;};
    struct TOKEN_PRIVILEGES {Int32 PriviligeCount; /*WHAT TO WRITE HERE??*/};


Help me please.

hkBattousai
Light Poster
33 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Hi

Would it not be something like;

LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];


for the above i have assumed that, you want an array of LUID_AND_ATTRIBUTES type struct , which you have defined before, as comprising of LUID Luid; int32 Attributes.
If my assunption is wrong, then please accept my apologies.

HTH

Thanks

I have this C++ code :

const int ANYSIZE_ARRAY = 1;
    struct LUID {DWORD LowPart; WORD HighPart;};
    struct LUID_AND_ATTRIBUTES {LUID Luid; DWORD Attributes;};
    struct TOKEN_PRIVILEGES {DWORD PrivilegeCount;   LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];};


I want to convert this code into C# syntax.

I tried this one, but don't know how to place array in a struct.

const int ANYSIZE_ARRAY = 1;
    struct LUID {Int32 LowPart; Int16 HighPart;};
    struct LUID_AND_ATTRIBUTES {LUID Luid; Int32 Attributes;};
    struct TOKEN_PRIVILEGES {Int32 PriviligeCount; /*WHAT TO WRITE HERE??*/};


Help me please.

brainbox
Light Poster
29 posts since May 2006
Reputation Points: 10
Solved Threads: 1
 
Would it not be something like; LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];


Given error :

Error    1    Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.    C:\Prog\SAVE\C#\Shutdown\Main.cs    22    86    Shutdown
Error    2    Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)    C:\Prog\SAVE\C#\Shutdown\Main.cs    22    87    Shutdown
for the above i have assumed that, you want an array of LUID_AND_ATTRIBUTES type struct , which you have defined before, as comprising of LUID Luid; int32 Attributes.


Exactly. What I try to do is place an array into a struct. I couldn't find an example not even in google.

This one doesn't work :

struct Animals { float Bird; int Rabbit[20]; Int64 Elephant; };


Error 1 Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type. C:\Prog\SAVE\C#\Shutdown\Main.cs 35 42 Shutdown
Error 2 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) C:\Prog\SAVE\C#\Shutdown\Main.cs 35 43 Shutdown

This one too :

struct Animals { float Bird; int[20] Rabbit; Int64 Elephant; };


Error 1 Array size cannot be specified in a variable declaration (try initializing with a 'new' expression) C:\Prog\SAVE\C#\Shutdown\Main.cs 35 36 Shutdown

Another one :

struct Animals { float Bird; int Rabbit[]; Int64 Elephant; };


Error 1 Syntax error, bad array declarator. To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type. C:\Prog\SAVE\C#\Shutdown\Main.cs 35 42 Shutdown

This one works :

struct Animals { float Bird; int[] Rabbit; Int64 Elephant; };


But now I am not able to specify the size of the array:sad:


Any suggestions?

hkBattousai
Light Poster
33 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

The answer is in the error message.

struct Animals { float Bird; int Rabbit[20]; Int64 Elephant; };

The previous code should be replaced with the following:

struct Animals { float Bird; fixed int Rabbit[20]; Int64 Elephant; };

Notice the addition of the keyword "fixed" which is place before the variable declaration.
See http://en.wikibooks.org/wiki/C_Sharp_Programming/Keywords/fixed for more details about the "fixed" keyword.

drjr01
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You