문제

I am practicing using pointers in C# (via unsafe code). So now, I just want to concatenate "" to an int*, so I can use it as a parameter in 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. So what should I do?

도움이 되었습니까?

해결책

If you want the memory address of the pointer.. cast it to an int:

Console.WriteLine((int)&value); // will produce random memory address

If you want the value of address.. dereference the pointer:

Console.WriteLine(*address); // produces 10

I have absolutely no idea why you're trying to concatenate anything with a string.. it isn't necessary.

다른 팁

&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:

Console.WriteLine(*adress + "  asd");

The output will be:

10 asd
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top