D
D
Daniil Demidko2015-12-11 07:17:27
C++ / C#
Daniil Demidko, 2015-12-11 07:17:27

Weirdness of char array initialization?

MinGW
We can initialize an array of type char like this:

// 4 конечно же завершающий символ
char example[4];

// вместе "name" Пока что все ясно. 4 по прежнему завершающий символ.
example[0]='n'; example[1]='a'; example[2]='m'; example[3]='e';

// Почему я не могу сделать так???
char exampleTwo[4]="name"; // Не компилируется!

// Зато вот так прекрасно компилируется:
char exampleTwo[5]="name"; // Два завершающих символа???

Who can explain to me what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2015-12-11
@Daniro_San

Found a mess in my head! Yours, unfortunately.
charexample[4]; - an array of 4 places. ON 4, CARL!
We count (in words) N (times) A ​​(two) M (three) E (four)
That's it, the place is over. There is no final character in place [4], this is your delusion.
To store a string of four characters, you need a five-character array. That's what the compiler required.
Conclusion - you - finish your studies. And the compiler is great.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question