#Does the length of a string include the null terminator?

19 messages · Page 1 of 1 (latest)

ornate bane
#

Lets say I had

char mystring[] = "cheese";

would the array length be 5 (how many letters are in cheese-1) or 6?

gloomy wharfBOT
#

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.

modest blaze
#

6, when you don't specify the length of the array and you use a string literal to initialize it, it includes the null terminator

#

wait it's 7, you tricked me lol

#

cheese is 6 letters

#

plus 1 for null terminator

ornate bane
#

oops

#

what if I did

#

[6]

#

would it not include the null terminator

#

or would it just error or

modest blaze
#

then it wouldn't include the null terminator because it doesn't fit into the array

ornate bane
#

ah I see I see

#

thank you!

#

!solved

gloomy wharfBOT
#

Thank you and let us know if you have any more questions!

modest blaze
#

np

ornate bane
#

!solved

gloomy wharfBOT