Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is difference between constant pointer and pointer to constant. Constant pointer is a pointer (a number - memory address) that cannot be changed - it always point to the same object given via initialization:</p> <pre><code>int * const const_pointer = &amp;some_int_var; // will be always pointing to this var const_pointer = &amp;some_other_var; // illegal - cannot change the pointer *const_pointer = 2; // legal, the pointer is a pointer to non-const </code></pre> <p>Pointer to constant is a pointer whose pointed value cannot be changed:</p> <pre><code>const int * pointer_to_const = &amp;some_int_var; // doesn't have to be always pointing to this var pointer = &amp;some_other_var; // legal, it's not a constant pointer and we can change it *pointer = 2; // illegal, pointed value cannot be changed </code></pre> <p>You can always assign constant to variable i.e. const pointer to non-const pointer (a). You can cast pointer to non-const to a pointer to const (b). But you cannot cast pointer to const to a pointer to non-const (c):</p> <pre><code>int * pointer; int * const const_pointer = &amp;var; const int * pointer_to_const; /* a */ pointer = const_pointer; // OK, no cast (same type) /* b */ pointer_to_const = pointer; // OK, casting 'int*' to 'const int*' /* c */ pointer = pointer_to_const; // Illegal, casting 'const int*' to 'int*' </code></pre> <p><em>[EDIT] Below, this is not standard c++. However, this is common.[/EDIT]<br /></em> String literal</p> <pre><code>"Hello" </code></pre> <p>is converted to constant pointer to const (<code>const char * const</code>):</p> <pre><code>char *pointer = "Hello"; // Illegal, cannot cast 'const char*' to 'char*' char * const const_pointer = "Hello"; // Illegal, cannot cast 'const char*' to 'char*' const char * pointer_to_const = "Hello"; // OK, we can assign a constant to a variable of the same type (and the type is 'const char*') "Hello" = pointer_to_const; // Illegal cannot re-assign a constant </code></pre> <p>In above examples the second is your case. You tried to initialize <strong>pointer-to-non-const</strong> with a <strong>pointer-to-const</strong> when passing string literal as argument of your function. No matter if these pointers are constants or not, it's matter what do they point to.</p> <p>Summary:<br /> 1) If you cast a pointer of some type to a pointer of another type, you cannot cast pointer-to-const to pointer-to-non-const.<br /> 2) If you have constant pointer, the same rules applies as to other constants - you can assign a constant to a variable but you cannot assign a variable to a constant (except initializing it).</p> <p>// EDIT <br /> As GMan pointed out, the C++98 standard (§4.2/2) allows to implicitly cast string literals (which are constant char arrays) to a non-const char pointer. This is because of backward compatibility (in C language there are no constants).</p> <p>Of course such a conversion can lead to mistakes and compilers will violate the rule and show an error. However, GCC in compatibility mode shows only a warning.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThis does contain some wrong information, leading to wrong results. A string literal is an [array of const char's](http://stackoverflow.com/questions/3303164/why-isnt-if-maya-maya-true-in-c/3303212#3303212), not a `const char*` (Though, since it's an array, it can decay into such). While it's true such an array cannot typically be implicitly converted to `char*`, string literals have this special conversion. (It's deprecated, and only exists for backwards compatibility.) So all three of your code examples are completely legal.
      singulars
    2. COI wrote that "it is converted to a pointer", not "it is a pointer". It is converted to a pointer that points to the array you are saying about. And you are wrong - string literals cannot be implicitly converted to 'char*'. Maybe you are confused with char array initialization.
      singulars
    3. CONo, I'm not. §4.2/2: "A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type “pointer to char”; a wide string literal can be converted to an rvalue of type “pointer to wchar_t”. In either case, the result is a pointer to the first element of the array. This conversion is considered only when there is an explicit appropriate pointer target type, and not when there is a general need to convert from an lvalue to an rvalue. [Note: this conversion is deprecated. See Annex D. ]" Like I said, all three lines are legal. (Well, you have four now; the first 3.)
      singulars
 

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