Where will the constant string be stored in memory?

sometimes we use this type of code in our C programming.

char *p = "Sam"; 

Here the address of the line with the constant character "Sam" will be stored in the char p pointer. now here
I want to ask where will Sam be kept?

+7
source share
2 answers

This is not specified in the standard. Typically, a string literal ( "Sam" ) will be stored in the data section of a read-only page.

As for p itself, it depends on whether it is automatic or static.

+9
source

The string "Sam" is usually stored in global memory in the same area as global constants.

However, if you did this:

 char p[] = "Sam"; 

Then it will be on the stack. (as an array initializer)

+8
source

All Articles