Runtime Error Check Failure

Can someone explain why this produce errors..

char temp[] = "";

temp[0] = 'a';
temp[1] = 'b';
temp[2] = 'c';
temp[3] = '\0';

please advice. thanks
Last edited on
Size of temp is just one char. If you try to write more, you go out of bounds and thus get an error.
You should declare char temp[4];
Though if you want work with strings, you should use std::string.
thanks. how about this? this displays an access violation(unhandled exception)

char* temp = "";

temp[0] = 'a';
temp[1] = 'b';
temp[2] = 'c';
temp[3] = '\0';
and by the way.. in that way. how can i make the variable temp be dynamic in such a way that i will not anymore set the size of the array..

thanks.. please advice.
The second one is no better. The type of string literals is const char*, even though compilers allow assigning them to normal char*. The thing is that you can't write to them at all. And even if you could, you'd still be writing out of bounds.
As I said, use std::string. You won't be able to immediately write wherever you want,but you car resize them at any time.
Topic archived. No new replies allowed.