C++ char array characteristic: why does a string stop before the end of the array?

C++ char array characteristic: why does a string stop before the end of the array?

When you enter your string with cin it automatically adds a at the n+1 element of the string (n being the size of your string). String operated functions like cout or fopen will only read up to that point. To test this theory you can enter a string 10 characters long, say, and then manually change the 11th character to something else. Then manually add a at say the 20th character and print out the string. Youll get a 20 char string with the chars from 10 to 20 being any gibberish.

fopen() uses c-string which by definition is a list characters ending to or NULL. It does not use array length.

When you read path using cin, it automatically adds a character at end of data. So when you try to open it with fopen() you only read it till there and ignore any other character after that .

C++ char array characteristic: why does a string stop before the end of the array?

Leave a Reply

Your email address will not be published. Required fields are marked *