The main difference is just the syntax you use to access your char.
By "access" I mean, act on it using various operators in this language, most or all of which do different things when applied to char
compared to the char
array. This makes the sound as if x
and y
almost completely different. If the fact that they both "consist of" one char, but that the char was presented in a completely different way.
Implementation can lead to other differences, for example, it can align and lay out the structure in different ways according to which you use. But I doubt it will be.
An example of operator differences is that a char can be assigned and the array is not equal:
SomeStruct a; ax = 'a'; ay[0] = 'a'; SomeStruct b; bx = ax; // OK by = ay; // not OK by[0] = ay[0]; // OK
But the fact that y
not assigned does not stop SomeStruct
, which can be assigned:
b = a; // OK
All this regardless of type, char
or not. An object of type and an array of this type with size 1 are almost the same in terms of what is in memory.
Aside, there is a context in which it matters a lot, which you βuseβ from char
and char[1]
, and which sometimes helps to confuse people with the idea that arrays are really pointers. Not your example, but as a function parameter:
void foo(char c);
The numbers given in the bar
and baz
declarations are completely ignored by the C ++ language. Apparently, at some point, someone thought it would be useful for programmers as a form of documentation, indicating that the baz
function expects its pointer argument to point to the first element of an array of 12 char.
In bar and baz, c
never has an array type - it looks like an array type, but it doesnβt, it's just a fancy special-case syntax with the same value as char *c
. That's why I put quotes in "use" - you don't use char[1]
, it just looks.