Jeegar Patel
Jeegar Patel

Reputation: 27210

Where will a constant string be stored in memory?

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

char *p = "Sam";

Here the address of constant character string "Sam" is going to be stored in char pointer p. now here
i want to ask where the Sam is going to be stored ?

Upvotes: 9

Views: 7134

Answers (2)

Mysticial
Mysticial

Reputation: 471199

The string "Sam" will usually be stored in global memory in the same region as the global constants.

However, if you did this:

char p[] = "Sam";

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

Upvotes: 10

cnicutar
cnicutar

Reputation: 182619

The standard doesn't specify this. Typically the string literal ("Sam") will be stored in the data section, in a read-only page.

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

Upvotes: 9

Related Questions