Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I have C++ library. I want to use it in C# project. I need to pass linked list to C++ function. My C++ structure is like this:
C++
struct Node
{
   char data[];
   struct Node *next;
};

Every time when I create an object of that structure, the next pointer will have to point to the previously created node. This is how a linked list will be formed, but when I am trying to write unsafe struct in C# like this:
C#
[StructLayout(LayoutKind.Sequential)]
unsafe struct Node
{
   char [] data = new char[10];
   struct Node *next;
};

I am getting an error while declaring the structure pointer inside the structure
Error - The type or namespace name "next" could not be found

Please let me know if i am missing something while marshaling.

Thank you,

Bipin
Posted
Updated 3-Feb-12 1:27am
v2
Comments
Manfred Rudolf Bihy 3-Feb-12 7:29am    
Added code tags, fixed small grammar and spelling mistakes.
@OP: When you put code into to your question be sure to always wrap that code into code tags (<pre lang="">).

Thanks for your cooperation!

1 solution

You don't need the struct keyword inside the declaration - just the Node* part:
C#
[StructLayout(LayoutKind.Sequential)]
unsafe struct Node
{
   char [] data = new char[10];
   Node *next;
};
 
Share this answer
 
Comments
dalbhide bipin 3-Feb-12 7:43am    
Then i am getting error
Can not take the address of, get the size of, or declare a pointer to managed type Node.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900