char* in C/C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

char* in C/C++

1. How does a pointer to a char can store a string that consists of multiple chars? 2. How does it know the length of the word? because it only knows the location of the first char of the string, but how does it know where does it end? Thanks.

19th Aug 2021, 11:30 AM
Yahel - avatar
1 Answer
+ 6
1. It doesn't, it just points to the first char of the string (char array) 2. It doesn't know the lenght and it doesn't know where it ends, but it knows how it ends A strings is a null-terminated array of chars, that means that at the end of every string there is a '\0', that's how it knows when to stop e.g.: "Hello" = ['H', 'e', 'l', 'l', 'o', '\0']
19th Aug 2021, 11:40 AM
Angelo - avatar