Character Arrays

To my understanding- a string is nothing more than an allocated space of an array of characters.

char A[15] = "Hello, World!";
char A[] = "Hello, World!";

Also to my understanding, when declaring a character array, and denoting the amount of space to allocate (char A[15])- this is the max amount of characters that the array can hold.

So one could safely assume that the compiler would throw an error at compile time if the "string" exceeded the length of the array.

But when I implement the following-

1
2
3
4
5
using namespace std;

char A[1] = "Hello, World!";

cout << A << endl;



There is no error thrown- and the code executes without fail. Am I missing something? Given the code above, I should only have enough space for 1 byte. Meaning technically I should only have room for 1 character.

Any help or suggestions would be great.
What compiler are you using? You're right in your assumption, you should get an error like
initializer-string for array of chars is too long
Topic archived. No new replies allowed.