Alex Rivera | Logout

Addressing the address of a string literal in C

Asked 2012-03-31T19:19:29.343
8
char **s = &"Is this valid?";

Is obtaining the address at which the address of a string literal is stored allowed in C? I know that the string literal is stored in the .rodata datasegment. However, obtaining the address of that address doesn't make sense.

It should be noted that gcc compiles this and produces a working executable.

Edit
Report

1 Answer

11

Your example is not valid:

char **s = &"Is this valid?";   // Not valid, wrong type

This is valid:

char (*s)[15] = &"Is this valid?";  // OK

The type of "Is this valid?" is char[15]. The type of a pointer to an array 15 of char is char (*)[15]. So the type of &"Is this valid?" is char (*)[15].

The type of a string literal is char[N+1] where N is the length of the string.

answered 2012-03-31T19:24:28.390

Your Answer