+ 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";
+ 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.
+ 1
I dont understand
+ 1
pls can someone explain
0
Nice