The difference between char and char [1]

In C ++, what is the difference (if any) between using char and char [1].

examples:

struct SomeStruct { char x; char y[1]; }; 

Perform the same reasons for unsigned char?

+54
c ++ arrays char
Nov 08 '10 at 1:20
source share
3 answers

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); // 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 

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.

+39
Nov 08 '10 at 1:28
source share

If you really saw the construction of char y[1] as the last member of the structure in production code, then it is quite likely that you are faced with an instance of the structure to crack .

This short array is a reserve for a real variable-length array (recall that before c99 there was no such thing in the c standard). The programmer always allocated such structures to a heap, making sure that the distribution was large enough for the actual size of the array that he wanted to use.

+11
Nov 08 '10 at 1:28
source share

As well as the distinctive usage differences emphasized by Steve, char [1] can be passed, for example, template <int N> void f(char(&a)[N]) , where char x = '\0'; f(&x); char x = '\0'; f(&x); does not match. The reliability of capturing array arguments is very convenient and encouraging.

It may also mean something else: either the actual length may be longer (as dmckee explains), or that logically is an ASCIIZ string (which in this case is empty) or an array of characters (which has one element). If the structure was one of several interconnected structures (for example, a mathematical vector where the size of the array was the template argument or the coding of the memory layout necessary for some I / O operation), then it is quite possible that there is some similarity with other fields where arrays may be larger, suggest a one-character array preference, allowing you to simplify and / or more universally use the support code.

+3
Nov 08 '10 at 3:15
source share



All Articles