c#中等效的'C'指针。 [英] Equivalent of 'C' pointer in c#.

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

问题描述

大家好,

我需要根据'C'结构创建结构。
下面提到的
'C'结构



typedef struct stacks

{

NL_F1DNODE * f1d;

} NL_STACKS;



其中NL_F1DNODE是

typedef struct f1dnode {

NL_FLAG * ptr;

struct f1dnode * next;

} NL_F1DNODE;



如何在c#中定义上述结构以及如果结构具有双*例如

Hi All,
I need to create the structure based on the 'C' structure.
'C' structure mentioned below

typedef struct stacks
{
NL_F1DNODE * f1d;
}NL_STACKS;

where NL_F1DNODE is
typedef struct f1dnode {
NL_FLAG *ptr;
struct f1dnode *next;
} NL_F1DNODE;

How we can define in c# the above structure and also If the structure has dual * for example

typedef struct f2dnode {
NL_FLAG ** ptr;
struct f2dnode *next;
} NL_F2DNODE;





请有人知道的意思是建议我。谢谢

.

Please anyone knows means suggest me.Thanks

推荐答案

C#中有指针,但只有 unsafe 代码: https://msdn.microsoft.com/en-us/library/y31yhkeb.aspx [ ^ ]。



但是,为了您的目的,您不需要指针,但需要参考。它们像指针一样工作,但不直接与内存一起工作;它们就像指针的管理类似物。

你有引用类型类型,这些类型总是在堆中。因此,当您创建类的实例时,始终创建对某些堆驻留对象的引用:

There are pointers in C#, but only in unsafe code: https://msdn.microsoft.com/en-us/library/y31yhkeb.aspx[^].

However, for your purpose you don't need pointers, but need references. They work like pointers, but don't work directly with memory; they are like the managed analogs of pointers.
You have reference-type types, those types are always in heap. So, when you create an instance of a class, you always create a reference to some heap-reside object:
class MyClass { internal int someMember; }
MyClass instance = new MyClass();



此时,有两个对象:对象本身,在堆中,它是name,它是引用堆中对象的对象,实例

要查看它是如何工作的,请创建另一个参考:


At this point, there are two objects: the object itself, in the heap, and it's "name", which is the object referencing the object in heap, instance.
To see how it works, create another reference:

MyClass instanceCopy = instance;



现在你有三个对象:引用同一个对象的两个引用。

要查看它是如何工作的,请尝试更改属性:


Now you have three objects: two references referencing the same object.
To see how it works, try to change the property:

instanceCopy.someMember = 13;
// no instance.someMember == 13;



你能看到它现在如何类似于指针吗?

现在,指针本身是独立的,但不是引用的对象,因为实际上只有一个引用的对象。例如:


Can you see how it resembles pointers now?
Now, the pointers themselves are independent, but not the referenced objects, because actually there is only one referenced object. For example:

instanceCopy = null;
// instance remains the same



你现在能看到它与指针的相似之处吗?



-SA


有在C#中没有指针,它是一种高级语言,并没有为您提供可以让您操作内存的工具。就像C中的malloc或free函数一样,没有这样的东西。同样,C#中没有指针(阅读其余的答案,让我澄清



C#是一种托管语言,意味着你只需要编写逻辑。地下程序由框架管理(.NET是最初的)。因此使用垃圾收集器。通常由于这个和语言结构,C#中没有引入指针。



但是,如果使用指针只是使用内存中对象的引用并处理内存中存在的对象而不是只使用副本对象。您可能需要使用 参考 [ ^ ]关键字。如果对象按值传递,则对象 by-reference 将引用。例如, int 类型是值类型。使用 ref 作为修饰符通过引用传递,与& a 相同(参考)。



There are no pointers in C#, it is a high-level language and doesn't provide you with facilities that can allow you to manipulate the memory. Like in C the malloc or free function, there are no such things. Similarly, there are no pointers in C# (Read the rest of answer, let me clarify)

C# is a managed language, means that you only have to write the logic. The underground procedures are managed by the framework (.NET was initial). Garbage Collectors are used for this reason. Generally because of this and the language structure, there were no pointers introduced in C#.

However, if using a pointer is just to use the reference of object from memory and work on the object that is present in the memory rather than working with just the copy of the object. You may want to use the ref[^] keyword. Ref would by the object by-reference, in cases where objects are passed by-value. For example, int types are value-types. Using a ref as a modifier would pass it by reference, same as &a (reference of a).

void AddFive (ref int a) {
   a + 5; // No need to return, it has already been updated.
}





如果这不能解决问题,那么C#仍然可以让你满意。只需添加 不安全 [ ^ ]修改器到你写不安全的函数(在托管语言中,unsafe意味着指针和引用,因为它们不受.NET框架管理)代码。



阅读MSDN文档了解更多信息。



If this doesn't solve the problem, then C# still has you covered. Just add unsafe[^] modifier to the function where you would write your unsafe (in managed language unsafe means pointer and references because they are not managed by .NET framework) code in it.

Read the MSDN document for more on that.


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

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