c++ - Confusion about the necessity of the null-character? -


i reading why there need null-characters, , found this answer made sense me. states needed because char arrays (for c strings) allocated larger actual strings , thereby need a way symbolize end.

but why aren't these array not constructed size deduction based on initializer (without null-character implicitly added when assigning directly string literals). like, if arrays holding strings constructed using size deduction, there not need null-character because array not bigger string, of course, end @ end of array.

i reading why there need null-characters, , found answer made sense me. states needed because char arrays (for c strings) allocated larger actual strings , thereby need a way symbolize end.

the answer misleading. that's not reason why null termination needed. accepted answer more upvotes better.

there not need null-character because array not bigger string, of course, end @ end of array.

let remind ourselves, cannot use arrays function arguments. if could, wouldn't want to, because slow copy entire array argument.

therefore, there need refer array indirectly. indirection commonly achieved using pointers (or references). now, have "pointer character array of size 42", not useful because argument can point strings of 1 particular size.

instead, common approach use pointer first element of array. common pattern language has rule allows name of array implicitly decay pointer first element.

but can tell how big array is, based on pointer element of array? cannot. need information. accepted answer of linked question explains options available representing size, , designer of c chose option uses terminating character (which convention used bcpl language c based on).


tl;dr size information needed because there need refer string indirectly, , indirection hides knowledge size of array. null termination 1 way encode size information within content of string, , way chosen designer of c language.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -