0

If I write char * p = "Welcome". I can see the address for p. But what's the address for the string i.e at which address Welcome stored?

If I write again char *s = "Welcome". p and s will point to same address?

Ben Ruijl
  • 4,625
  • 3
  • 30
  • 39
Ramana
  • 32
  • 5

3 Answers3

2

In a debugger, if you inspect p, you will see the address of the string.

&p is the address of p itself.

And no, p and s are not guaranteed to point to the same address, but they might.

Luchian Grigore
  • 245,575
  • 61
  • 446
  • 609
0

"Welcome" is string constant and it is stored in read only data section of memory but pointer p is created in stack which points to this string literal

Omkant
  • 8,730
  • 7
  • 37
  • 59
  • The question does not imply anyhow that p is a local variable. It can be in the data section as well (writable) and not on the stack. – Alexey Frunze Sep 14 '12 at 12:02
  • Thanks..How to print the address of this const string? *p gives the the string value and &p gives p address.Sorry to bother you guys. Printing *p with %s gives string and %x giving the address. Hope what comes on print with %x is the address of constant string. – Ramana Sep 14 '12 at 12:22
0

String constant "Welcome" often are putted in "read-only-data" section of memory. Here are good explanations about: String litereals where do they go and data segment

you can find the address of string constant "Welcome" by

 printf("%p",p);

If I write again char *s = "Welcome". p and s will point to same address?

Maybe same string constant are putted in the same address, maybe not.

Community
  • 1
  • 1
nofomopls
  • 321
  • 3
  • 16