Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="https://stackoverflow.com/questions/20467514/c-char-to-char-conversion#comment30585757_20467514">Jefffrey's comment</a> references the standard, here it is:</p> <blockquote> <p>4.2 Array-to-pointer conversion [conv.array] </p> <p>An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.</p> </blockquote> <p>And a prvalue is:</p> <blockquote> <p>A prvalue ("pure" rvalue) is an expression that identifies a temporary object (or a subobject thereof) or is a value not associated with any object.</p> </blockquote> <p>You cannot bind a non-const reference to a temporary.</p> <pre><code>int&amp; i = int(); // error char* argv[] = { "", "", nullptr }; // the result of the conversion is a prvalue char**&amp; test = argv; // error </code></pre> <p>Therefore the following code will happily compile:</p> <pre><code>#include &lt;iostream&gt; void f(int&amp; argc, char** const&amp; argv){ std::cout &lt;&lt; argv[0] &lt;&lt; std::endl; // a } int main() { int argc = 2; char* argv[] = { "a", "b", nullptr }; f(argc, argv); return 0; } </code></pre> <hr /> <p>One important thing I glazed over is pointed out in <a href="https://stackoverflow.com/questions/20467514/c-char-to-char-conversion/20469655?noredirect=1#comment30596711_20469655">Kanze's comment</a>.</p> <p>In the first example provided in the OP, <code>char* argv[]</code> and <code>char** argv</code> are equivalent. Therefore, there is no conversion.</p> <pre><code>std::cout &lt;&lt; std::is_array&lt;decltype(argv)&gt;::value &lt;&lt; std::endl; // false std::cout &lt;&lt; std::is_array&lt;char**&gt;::value &lt;&lt; std::endl; // false std::cout &lt;&lt; std::is_array&lt;char*[]&gt;::value &lt;&lt; std::endl; // true std::cout &lt;&lt; std::is_same&lt;decltype(argv), char**&gt;::value &lt;&lt; std::endl; // true std::cout &lt;&lt; std::is_same&lt;decltype(argv), char*[]&gt;::value &lt;&lt; std::endl; // false </code></pre>
 

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