Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off, let's fix your array size problem:</p> <p>Character arrays in C are <a href="http://en.wikipedia.org/wiki/Null-terminated_string" rel="nofollow noreferrer">null-terminated:</a> This means that you need to add space for a trailing <code>\0</code> to terminate your string. You could use <code>char arr[5]</code> on the first line. (Note that if you are <strong>ABSOLUTELY CERTAIN</strong> you are <strong>NEVER</strong> going to use this array of characters with any of the C string handling functions, <strong>AND</strong> you assigned your characters individual as chars instead of as a string, this is not technically required. But save yourself some debugging time and use up the extra byte.) This is probably the source of your "weird error." </p> <p>It seems like you know, but the other thing to bear in mind is in C, arrays are <a href="https://softwareengineering.stackexchange.com/questions/110804/why-are-zero-based-arrays-the-norm">zero-based</a>. This means that when you declare and array like <code>char arr[4]</code>, you really get </p> <pre><code>arr[0] arr[1] arr[2] arr[3] </code></pre> <p>C has no qualms about letting you walk off the end of your array and stomp on data or read in bad values. Here be dragons. Be careful.</p> <p>Now, on to your actual questions:</p> <p>1) You assign actual characters using single quotes <code>arr[2]='x';</code> If you use double quotes, you are assigning a C string, which is null-terminated, as discussed.</p> <p>2) Yes, <code>printf</code> with <code>%c</code> should do the trick.</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