0

For example,

char * first = "Hello World!"; 

My questions is,

  1. Is the string literal stored in memory? (If it is I guess the pointer first is the address of inital element of the string literal "Hello World")

  2. If not, then is some kind of random pointer value stored in first?

4
  • 3
    1. Yes. 2. Moot, since the first answer is "yes". Commented Aug 29, 2016 at 5:20
  • What do you mean by saving a pointer? Commented Aug 29, 2016 at 5:21
  • @RawN I think the word "define" is more suitable ! Commented Aug 29, 2016 at 5:22
  • @RawN I think he means store the pointer in first. Commented Aug 29, 2016 at 5:23

3 Answers 3

3

Yes, string literals are stored in memory. The C and C++ standards just say that string literals have static storage duration, any attempt at modifying them gives undefined behavior, and multiple string literals with the same contents may or may not share the same storage.

Sign up to request clarification or add additional context in comments.

Comments

3

Yes the string literal is stored in memory, usually it's stored in the .rodata section

Comments

1

It depends on your platform and it is implementation-defined. In general it goes to the read-only memory, if available on your system. Read more here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.