Can someone please explain me why its not possible to put a '\0' character in the given array:
char a[]={'r','b'};
a[2]='\0';
Shouldn’t the above code put a null character at the third slot and hence convert the character array a to a character string.
You are writing past the array boundary: when you initialize an array with two characters, the last valid index is
1, not2.You should initialize your array with three items, as follows:
You could as well use this version:
This will give you a writable array with a zero-terminated string inside.