How int * is bool in c #?

I am trying to use pointers in C #, as in C.

public static void Main(string[] args)
    {
        unsafe
        {
            int i = 5;
            int* j = &i;
            Console.WriteLine(j);  //cannot convert from 'int*' to 'bool'
        } 

    }

I have two queries regarding this code:

  • How to compile / insecure?
  • Why is Console.Writeline(j)trying to convert j to bool?
+3
source share
3 answers

Point 2) takes 2 steps:

  • Console.WriteLine() no overload for int*
  • Apparently, he is trying to overload first, WriteLine(bool value)and cannot find the conversion

I would call this a weak error message.

To print an address, first translate it into ulong.

+11
source
  • If you use csc add / unsafe; or in a vs project, go to the project properties and check the box
  • ; , , " , , "
+3

Writing a pointer value is pointless; it's just a random address. You initialize the pointer with the address of the variable on the stack, such an address does not repeat well. You want to dereference a pointer using the * operator. Like this:

        Console.WriteLine(*j);

Which writes the specified value, 5 in your case.

+2
source

Source: https://habr.com/ru/post/1787312/


All Articles