+ 15
in character array '\0' null character is necessary like this char ch[]={'h','e','l','l','o','\0'}; in string literals ‘\0’ is not necessary. C inserts the null character automatically.A string is a class that contains a char array, but automatically manages it for you.  char ch[]="hello";
2nd Feb 2018, 5:14 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 9
According to my understanding, C-string are constant character pointers which point to the first character of a string literal, which is stored in read-only memory. const char* p = "hello"; An array of characters, on the other hand, when used to store a string literal, allocates memory on the stack and then copies the characters in the string literal over into the allocated memory. This means that the character array does not point to the first character of the string literal, but rather the first character in the newly allocated memory space. char p2[] = "hello"; const char* p = "hello"; p[1] = 'i'; std::cout << p; // The above results in an error, because string literals are read-only and cannot be modified. char p2[] = "hello"; p2[1] = 'i'; std::cout << p2; // This one, on the other hand, is fine because the modification of the contents within the character array does not affect the string literal.
2nd Feb 2018, 5:16 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
I dont understand
2nd Feb 2018, 7:56 PM
Android Boi
Android Boi - avatar
+ 1
pls can someone explain
2nd Feb 2018, 7:56 PM
Android Boi
Android Boi - avatar
0
Nice
29th Oct 2019, 1:19 AM
Wasif Ur Rehman