Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The main difference is just the syntax you use to access your one char.</p> <p>By "access" I mean, act upon it using the various operators in the language, most or all of which do different things when applied to a <code>char</code> compared with a <code>char</code> array. This makes it sound as if <code>x</code> and <code>y</code> are almost entirely different. If fact they both "consist of" one char, but that char has been represented in a very different way.</p> <p>The implementation <em>could</em> cause there to be other differences, for example it could align and pad the structure differently according to which one you use. But I doubt it will.</p> <p>An example of the operator differences is that a char is assignable, and an array isn't:</p> <pre><code>SomeStruct a; a.x = 'a'; a.y[0] = 'a'; SomeStruct b; b.x = a.x; // OK b.y = a.y; // not OK b.y[0] = a.y[0]; // OK </code></pre> <p>But the fact that <code>y</code> isn't assignable doesn't stop <code>SomeStruct</code> being assignable:</p> <pre><code>b = a; // OK </code></pre> <p>All this is regardless of the type, <code>char</code> or not. An object of a type, and an array of that type with size 1, are pretty much the same in terms of what's in memory.</p> <p>As an aside, there is a context in which it makes a big difference which you "use" out of <code>char</code> and <code>char[1]</code>, and which sometimes helps confuse people into thinking that arrays are really pointers. Not your example, but as a function parameter:</p> <pre><code>void foo(char c); // a function which takes a char as a parameter void bar(char c[1]); // a function which takes a char* as a parameter void baz(char c[12]); // also a function which takes a char* as a parameter </code></pre> <p>The numbers provided in the declarations of <code>bar</code> and <code>baz</code> are completely ignored by the C++ language. Apparently someone at some point felt that it would be useful to programmers as a form of documentation, indicating that the function <code>baz</code> is expecting its pointer argument to point to the first element of an array of 12 char.</p> <p>In bar and baz, <code>c</code> never has array type - it looks like an array type, but it isn't, it's just a fancy special-case syntax with the same meaning as <code>char *c</code>. Which is why I put the quotation marks on "use" - you aren't really using <code>char[1]</code> at all, it just looks like it.</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