指针和固定大小的缓冲区 [英] Pointers and fixed size buffers
本文介绍了指针和固定大小的缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
希望可以有人帮帮我.我有以下代码:
Hi guys
Hope someone can help me. I have the following code :
void responseCode (char challenge0,
char challenge1,
char challenge2,
char challenge3,
char challenge4,
char challenge5,
char* response0,
char* response1,
char* response2,
char* response3,
char* response4,
char* response5)
{
char b0, b1, b2, b3, b4, b5;
b0 = byteConv (challenge0, 0);
b1 = byteConv (challenge1, 1);
b2 = byteConv (challenge2, 2);
b3 = byteConv (challenge3, 3);
b4 = byteConv (challenge4, 4);
b5 = byteConv (challenge5, 5);
*response0 = Convert.ToChar((b1 + b2 - b3 + b4 - b5) ^ b0);
*response1 = Convert.ToChar((b2 + b3 - b4 + b5 - b0) ^ b1);
*response2 = Convert.ToChar((b3 + b4 - b5 + b0 - b1) ^ b2);
*response3 = Convert.ToChar((b4 + b5 - b0 + b1 - b2) ^ b3);
*response4 = Convert.ToChar((b5 + b0 - b1 + b2 - b3) ^ b4);
*response5 = Convert.ToChar((b0 + b1 - b2 + b3 - b4) ^ b5);
}
但是我在这条线上出现错误:char * response0,
读取错误:指针和固定大小的缓冲区只能在不安全的上下文中使用.
如果有人可以帮助,它将被申请
在此先感谢
However I get an error on this line : char* response0,
The error reads : Pointers and fixed size buffers may only be used in an unsafe context.
If anyone can assist it will be appriciated
Thanks in advance
推荐答案
在C#中,仅当您使用不安全的方法时,才可以使用指针:
In C#, you can only use pointers when you are in an unsafe method:
private unsafe void responseCode (char challenge0,...
{
...
}
您还必须在项目属性中指定允许不安全的代码".
这是因为在C#中实际上不建议使用指针-在99.9999%的时间内,您不需要使用指针,因为引用可以完成工作.
You also have to specify "Allow unsafe code" in the project properties.
This is because pointers are really discouraged in C# - 99.9999% of the time, you do not need to use them, as references will do the job.
首先,应该允许不安全的指针;这是项目的财产.
在执行此操作时,请使用unsafe
语句:
http://msdn.microsoft.com/en-us/library/chfa2zb8%28v = vs.100%29.aspx [ ^ ].
学习本教程:
http://msdn.microsoft.com/en-us/library/aa288474%28v = vs.71%29.aspx [ ^ ].
最后,问问自己:为什么要尝试使用指针?通常,出于性能原因,与某些非托管代码进行互操作时需要使用此命令,在极少数情况下,需要这样做.
—SA
First of all, you should allow unsafe; this is the property of the project.
When you do it, useunsafe
statement:
http://msdn.microsoft.com/en-us/library/chfa2zb8%28v=vs.100%29.aspx[^].
Study this tutorial:
http://msdn.microsoft.com/en-us/library/aa288474%28v=vs.71%29.aspx[^].
Finally, ask yourself: why are you trying to use pointer? Usually, this is needed for interoperation with some unmanaged code, and in much more rare cases, for performance reasons.
—SA
这篇关于指针和固定大小的缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文