I'm trying to understand why the following string passing works for my error string. I have made this example from a much bigger source I have.
My question is; why don't I have to specifically allocate memory for the char array which contains my error message? I would have thought that I need to malloc some memory for the string and use the err pointer to indicate the start of this memory.
Is this to do with that fact that its a const char *
or is is because I'm printing to stderr?
I'm possibly wording the question wrong which is why searches haven't helped me to understand this.
const char * my_function(int a)
{
if (a != 1)
return "a doesn't equal 1!"
else
return NULL;
}
int main(int a)
{
const char *err;
err = my_function(a);
if (err)
fprintf(stderr, "Message = %s\n",err);
return 1;
return 0;
}