when we define a string like
char *name = some_name;
and then print it
printf("your name is %s",name);
how does know where to stop while printing as we didn't include a '/0' at the end?
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.
Sign up to join this communitywhen we define a string like
char *name = some_name;
and then print it
printf("your name is %s",name);
how does know where to stop while printing as we didn't include a '/0' at the end?
Character pointers are normally used to build literal strings, the most usual way is to make a statement of the following type:
char *name = "my name is very long";
Some things you need to know about this statement.
It is enclosed in double quotes.
The pointer name points to the first elements of the string.
It is read-only or constant, if we try to change the value of name doing, for example name [0] = 'w', it will cause a segfault.
The string ends automatically in the character '\0'.
It is convenient to read something about the difference between a variable of type char * and an array of characters of type
char name [3] = {'a', 'b', 'c'};
But if you have doubts and do not see it clearly I suggest a new question, suggestion: it is always a good idea to learn by yourself, only when it is impossible to move forward you can ask.