Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Now strPtr and strArray are considered to be string literals.</p> </blockquote> <p>No, they aren't. String literals are the things you see in your code. For example, the <code>"Hello"</code>. <code>strPtr</code> is a <em>pointer</em> to the literal (which is now compiled in the executable). Note that it should be <code>const char *</code>; you cannot legally remove the <code>const</code> per the C standard and expect defined behavior when using it. <code>strArray</code> is an array containing a copy of the literal (compiled in the execuable).</p> <blockquote> <p>Both the above statements should be illegal. compiler should throw errors in both cases.</p> </blockquote> <p>No, it shouldn't. The two statements are completely legal. Due to circumstance, the first one is undefined. It would be an error if they were pointers to <code>const char</code>s, though.</p> <p>As far as I know, string literals may be defined the same way as other literals and constants. However, there are differences:</p> <pre><code>// These copy from ROM to RAM at run-time: char myString[] = "hello"; const int myInt = 42; float myFloats[] = { 3.1, 4.1, 5.9 }; // These copy a pointer to some data in ROM at run-time: const char *myString2 = "hello"; const float *myFloats2 = { 3.1, 4.1, 5.9 }; char *myString3 = "hello"; // Legal, but... myString3[0] = 'j'; // Undefined behavior! (Most likely segfaults.) </code></pre> <p>My use of ROM and RAM here are general. If the platform is only RAM (e.g. most Nintendo DS programs) then const data <em>may</em> be in RAM. Writes are still undefined, though. The location of const data shouldn't matter for a normal C++ programmer.</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. 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