Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I can't really remember if this ever worked in C#, but I am quite certain it should not work now.</p> <p>To start off by answering your question. There is no automatic cast between pointers and bool. You need to write </p> <pre><code>if(str != null) </code></pre> <p>Secondly, you can't convert char to bool. Moreover, there is no terminating character for C# strings, so you can't even implement this. Normally, you would write:</p> <pre><code>while(*end != '\0') // not correct, for illustration only </code></pre> <p>But there is no '\0' char, or any other magic-termination-char. So you will need to take an int param for length.</p> <p>Going back to the big picture, this sort of code seems like a terribly inappropriate place to start learning C#. It's way too low level, few C# programmers deal with pointers, chars and unsafe contexts on a daily basis.</p> <p>... and if you must know how to fix your current code, here's a working program:</p> <pre><code>unsafe public static void Main(string[] args) { var str = "Hello, World"; fixed(char* chr = str){ reverse(chr, str.Length); } } unsafe void reverse(char *str, int length) { char* end = str; char tmp; if (str != null) //Cannot implicitly convert type 'char*' to 'bool' { for(int i = 0; i &lt; length; ++i) //Cannot implicitly convert type 'char*' to 'bool' { ++end; } --end; while(str&lt;end) { tmp = *str; *str = *end; *end = tmp; --end; ++str; } } } </code></pre> <p>Edit: removed a couple of .Dump() calls, as I was trying it out in LINQPad :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload