#Strings

17 messages · Page 1 of 1 (latest)

vale minnow
#

Hello,
when I initialize a new string, do I have to consider the last character '\0' by adding 1 to the length of the string ?
Imagine I want to initialize a string of 12 characters.
Do I have to do :
char mystring[12] or char mystring[13]
Thanks !

frozen patrolBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

ivory chasm
#
char hello[5] = "hello"; // does not contain '\0'
char hello[6] = "hello"; // does contain '\0'
char hello[] = "hello"; // does contain '\0'
const char *hello = "hello"; // does contain '\0'
lyric bough
late tree
#
char hello[5] = "hello";
#

It does T_T

unkempt isleBOT
# late tree ``` char hello[5] = "hello"; ```
Compiler Output
<source>: In function 'main':
<source>:3:6: error: unused variable 'hello' [-Werror=unused-variable]
    3 | char hello[5] = "hello";
      |      ^~~~~
cc1: all warnings being treated as errors
Build failed
unkempt isleBOT
# late tree ``` char hello[5] = "hello"; ```
Compiler Output
<source>:3:6: error: unused variable 'hello' [-Werror,-Wunused-variable]
char hello[5] = "hello";
     ^
1 error generated.
Build failed
late tree
#

Neither GCC nor clang warn on it

#

They do however, if the array is even smaller - surprisingly it's still valid

ivory chasm
#

tbh this really isn't the way to do it... but bing_shrug

frozen patrolBOT
#

@vale minnow Has your question been resolved? If so, run !solved :)

vale minnow
#

!solved