Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They do two different things.</p> <pre><code>char str1[] = "Hello"; </code></pre> <p>is equivalent to</p> <pre><code>char str1[6] = "Hello"; </code></pre> <p>It makes <code>str1</code> an array of 6 <code>char</code>s, initialized to <code>"Hello"</code> plus a <code>'\0'</code> terminator.</p> <p>This:</p> <pre><code>char *str2 = "World"; </code></pre> <p>makes <code>str2</code> a <em>pointer</em> to <code>char</code>, pointing to the first element of a statically allocated read-only array of 6 <code>char</code>s, which is initialized to <code>"World"</code> plus a <code>'\0'</code> terminator.</p> <p>I mentioned that the array is read-only -- but for historical reasons it's not actually <code>const</code>; attempting to change it has undefined behavior. You should define it as <code>const</code> so the compiler will warn you if you incorrectly attempt to modify it:</p> <pre><code>const char *str2 = "World"; </code></pre> <p><code>str1</code> and <code>str2</code> can be used interchangeably in some, but not all, contexts. For example, either <code>puts(str1)</code> or <code>puts(str2)</code> will print the corresponding string to standard output (followed by a newline). This is because an array name, in most contexts, "decays" to a pointer to the array's first element.</p> <p>Since <code>str1</code> is an array, you can apply <code>sizeof</code> to it to determine how big the array is; <code>sizeof str2</code> gives you the size of a <code>char*</code> pointer, which probably won't be useful.</p> <p>With the first declaration, you can modify the characters of the array, but you can't change its size, and you can't make <code>str1</code> refer to a different array.</p> <p>With the second declaration, you can't modify the characters of the array, but you can change the pointer value itself so it points to some other array, or to nothing if you do <code>str2 = NULL;</code>.</p> <p>If all you want to do with <code>str1</code> or <code>str2</code> is pass it to something that expects a <code>char*</code> that points to a string, you can use either form. The pointer form makes it a bit clearer that you're not going to be modifying the string. You can even write:</p> <pre><code>const char *const str2 = "World"; </code></pre> <p>if you want to prevent modifying either the string or the pointer.</p> <p>If you want to do something else, one form or the other may have some advantages, but I can't comment further without knowing what you're using it for.</p> <p>There shouldn't be any significant difference in efficiency. Concentrate first on what how you want your code to behave. Adding <code>const</code> to anything you don't intend to modify helps document your intent and may help the compiler generate better code (because it doesn't need to assume that something may have been modified).</p> <p>Recommended reading: the <a href="http://www.c-faq.com/" rel="nofollow">comp.lang.c FAQ</a>, particularly section 6 which covers arrays and pointers.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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