S
s0suk3
Hi,
I've heard that a string literal has type 'char *' (i.e., a pointer to
a char object). But I'm confused as to why this works:
char *GetString(void)
{
return "hello";
}
int main(void)
{
printf("%s\n", GetString());
return 0;
}
And it does print 'hello'. But if the pointer that GetString() returns
points to a local object, wouldn't the buffer be deallocated when the
function returns, thus making the pointer point to nothing? Or does
that mean that a string literal has type 'static char *'? Or more
precisely, 'static const char *'?
Thanks,
Sebastian
I've heard that a string literal has type 'char *' (i.e., a pointer to
a char object). But I'm confused as to why this works:
char *GetString(void)
{
return "hello";
}
int main(void)
{
printf("%s\n", GetString());
return 0;
}
And it does print 'hello'. But if the pointer that GetString() returns
points to a local object, wouldn't the buffer be deallocated when the
function returns, thus making the pointer point to nothing? Or does
that mean that a string literal has type 'static char *'? Or more
precisely, 'static const char *'?
Thanks,
Sebastian