Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>char *strPtr ="Hello" ; </code></pre> <p>Defines <code>strPtr</code> a pointer to char pointing to a string literal <code>"Hello"</code> -- the effective type of this pointer is <code>const char *</code>. No modification allowed through <code>strPtr</code> to the pointee (invokes UB if you try to do so). This is a backward compatibility feature for older C code. This convention is deprecated in C++0x. See Annex C:</p> <blockquote> <p><strong>Change: String literals made const</strong> The type of a string literal is changed from “array of char” to “array of const char.” [...] </p> <p><strong>Rationale:</strong> This avoids calling an inappropriate overloaded function, which might expect to be able to modify its argument. </p> <p><strong>Effect on original feature:</strong> Change to semantics of well-defined feature. Difficulty of converting: Simple syntactic transformation, because string literals can be converted to char*; (4.2). The most common cases are handled by a new but deprecated standard conversion:</p> <p><code>char* p = "abc"; // valid in C, deprecated in C++</code></p> <p><code>char* q = expr ? "abc" : "de"; // valid in C, invalid in C++</code></p> <p><strong>How widely used:</strong> Programs that have a legitimate reason to treat string literals as pointers to potentially modifiable memory are probably rare.</p> </blockquote> <pre><code>char strArray[] ="Hello"; </code></pre> <p>The declared type of <code>strPtr</code> is -- it is an array of characters of unspecified size containing the string <code>Hello</code> including the null terminator i.e. 6 characters. However, the initialization makes it a complete type and it's type is array of 6 characters. Modification via <code>strPtr</code> is okay.</p> <blockquote> <p>Where exactly do string literals are stored ?</p> </blockquote> <p>Implementation defined.</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