Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't I convert 'char**' to a 'const char* const*' in C?
    text
    copied!<p>The following code snippet (correctly) gives a warning in C and an error in C++ (using gcc &amp; g++ respectively, tested with versions 3.4.5 and 4.2.1; MSVC does not seem to care):</p> <pre><code>char **a; const char** b = a; </code></pre> <p>I can understand and accept this.<br> The C++ solution to this problem is to change b to be a const char * const *, which disallows reassignment of the pointers and prevents you from circumventing const-correctness (<a href="http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17" rel="noreferrer">C++ FAQ</a>).<br></p> <pre><code>char **a; const char* const* b = a; </code></pre> <p>However, in pure C, the corrected version (using const char * const *) still gives a warning, and I don't understand why. Is there a way to get around this without using a cast?</p> <p>To clarify:<br> 1) Why does this generate a warning in C? It should be entirely const-safe, and the C++ compiler seems to recognize it as such.<br> 2) What is the correct way to go about accepting this char** as a parameter while saying (and having the compiler enforce) that I will not be modifying the characters it points to? For example, if I wanted to write a function:</p> <pre><code>void f(const char* const* in) { // Only reads the data from in, does not write to it } </code></pre> <p>And I wanted to invoke it on a char**, what would be the correct type for the parameter?</p> <p>Edit: Thank you to those who have responded, particularly those who addressed the question and/or followed up on my responses.</p> <p>I've accepted the answer that what I want to do cannot be done without a cast, regardless of whether or not it should be possible.</p>
 

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