[英]How do I concatenate a string to an int*?
I am practicing using pointers in C# (via unsafe code). 我正在练习在C#中使用指针(通过不安全的代码)。 So now, I just want to concatenate "" to an int*, so I can use it as a parameter in
Console.WriteLine()
. 因此,现在,我只想将“”连接为一个int *,因此可以将其用作
Console.WriteLine()
的参数。
static void Main(string[] args)
{
fullOfPointers();
}
static unsafe void fullOfPointers()
{
int value = 10;
int* adress = &value;
Console.WriteLine(&value+"");//error
Console.ReadLine();
}
But the compiler says that operator '+' cannot be used on int* and string. 但是编译器说运算符'+'不能用于int *和string。 So what should I do?
所以我该怎么做?
If you want the memory address of the pointer.. cast it to an int
: 如果您想要指针的内存地址..将其
int
转换为int
:
Console.WriteLine((int)&value); // will produce random memory address
If you want the value of address
.. dereference the pointer: 如果您希望
address
..的值取消引用指针:
Console.WriteLine(*address); // produces 10
I have absolutely no idea why you're trying to concatenate anything with a string
.. it isn't necessary. 我完全不知道为什么您要尝试用
string
连接任何内容..这是没有必要的。
&value
returns value's address
, you are trying to concatenate a memory address with a string literal.If you want to adress's
value, you need to use *address
, so you can try: &value
返回value的address
,您正在尝试用字符串文字连接一个内存地址。如果要获取adress's
值,则需要使用*address
,因此可以尝试:
Console.WriteLine(*adress + " asd");
The output will be: 输出将是:
10 asd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.