hello. I dont see any point of NULL in cstring. The code given below just outputs same as it would have done with NULL. My understanding is if size of char array is less than length of char array then null must be manually added?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main(){
char chr[0];
cin>>chr;//or if you use cin.getline;
cout<<chr<<endl;
return 0;
}
Enter something: Hellowww
Hellowww
but if I enter
1 2 3
Enter something: hellowwwww
hellowwwww
Segmentation fault (core dumped)
I guess you mean the null character '\0'. The null character is needed because that's how you can know the length of the string.
The >> operator automatically adds the null character at the end of the string so that is not an issue in your program.
The big problem with your program is that you are storing the string in an array of zero length so you can't store anything in it, not even the null character. The >> operator doesn't know the length of the array so it will just write to it as if it was unlimited. If the array is not big enough it will write to the memory after the array which could be used by other things or might not be useable at all. This might lead to strange bugs and crashes.