C#中的结构指针 [英] Structure pointer in C#

查看:119
本文介绍了C#中的结构指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有C ++库.我想在C#项目中使用它.我需要将链接列表传递给C ++函数.我的C ++结构是这样的:

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:

struct Node
{
   char data[];
   struct Node *next;
};


每当我创建具有该结构的对象时,下一个指针将必须指向先前创建的节点.这就是链表的形成方式,但是当我试图用C#这样编写不安全的结构时:


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:

[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


如果我在编组邮件时想念什么,请告诉我.

谢谢

Bipin


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

Thank you,

Bipin

推荐答案

您不需要在声明内使用struct关键字-只需Node *部分:
You don''t need the struct keyword inside the declaration - just the Node* part:
[StructLayout(LayoutKind.Sequential)]
unsafe struct Node
{
   char [] data = new char[10];
   Node *next;
};


这篇关于C#中的结构指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆