Surya
Surya

Reputation: 147

Where does the pointer to string stored

void main()
{
    char *p = "hello";
}

What is the storage type of P and where points in memory(stack/data segment)? Where the string "helllo" stored?

Upvotes: 1

Views: 213

Answers (4)

John Bode
John Bode

Reputation: 123468

Unless your compiler documentation explicitly says that void main() is a legal signature, use int main(void) instead:

int main(void)
{
  char *p = "hello";
  return 0;
}

Exactly where the memory for p and the string "hello" are allocated will vary with the implementation. For both ELF and PE/COFF formats, the memory for p will be inside of the stack frame for main and the memory for "hello" will be in a read-only data segment (.rdata for PE/COFF, .rodata for ELF).

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 612993

p is a local variable and typically resides on the stack.

The string is stored wherever the compiler decides to store it. Typically it will be in neither stack nor heap but rather in a read only area of the data segment of the executable image.

Upvotes: 5

Bence Ignácz
Bence Ignácz

Reputation: 106

Your string sotred in the memory, and the pointer refer to the memory address wehere the string is stored. If you call this pointer it's return whit the memory address, and you can use this.

Upvotes: 0

cnicutar
cnicutar

Reputation: 182649

The string is stored in read-only memory. The pointer itself is stored on the stack of main.

Upvotes: 2

Related Questions