Variable Area C

Possible duplicate:
Returns a string literal address from a safe and portable function?
"life" of a string literal in C

Hi i'm a little confused

char *func()
 {

    return "Hello";
 }

Here "Hello" is a sequence / array of characters. This is a local variable, and it should disappear as soon as the function returns. Then how is it that we can get the right value?

+5
source share
4 answers

"Hello"is a string literal and will exist for the lifetime of the program. To quote the relevant sections of the C99 standard:

  • 6.4.5 String literals

... , , ...

  • 6.2.4

, , static . - , .

const char*, - undefined.

+10

.

+1

.

, , return, "Hello" , ;

0

: ?

( malloc() free()), , . .

-1
source

All Articles