0

I would like to get the address of an string-constant in C.

 char * const MYCONST = "StringString";

As far as I know consts are "saved" in the Text/Code Segment of the memory. When I try to get the address of the first element in MYCONSt:

 printf("%p\n",&(MYCONST));

As result I get 0x7fff15342e28, which is in the stack and not in the Text/Code segement. Can anybody please help me get the address of a string-constant in C?

//edit I can't find the correct answer so far: When I write

  char * const MYCONST1 = "StringString";
  printf("Address of MYCONST1: %p\n",MYCONST1);

  char * const MYCONST2 = "StringString";
  printf("Address of MYCONST2: %p\n",(void*)MYCONST2);

this is the output:

Address of MYCONST1: 0x400b91

Address of MYCONST2: 0x400b91

But they should have different addresses, because the are different constants. Can anybody explain me while the result has a length of seven and not 0x7fffa5dd398c like a locale variable.

Thanks!

John Smithv1
  • 673
  • 5
  • 14
  • 33

6 Answers6

6

Since MYCONST is already a pointer, you do not need an ampersand. All you need is a cast to void* for the %p:

printf("%p\n",(void*)MYCONST);

With an ampersand, you print the address of the MYCONST local variable (you need a void* cast there as well, otherwise the address may print incorrectly), which is indeed located on the stack.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3
printf("%p\n",(void *) &MYCONST);

prints the address of the MYCONST pointer variable.

printf("%p\n", (void *) MYCONST);

prints the value of the MYCONST pointer variable.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • When I try to get the address of the pointer value: 0x7fff70f54bb8. I think that can't be correct, because constants are in the code segment and this address belongs to stack? – John Smithv1 Oct 12 '13 at 11:32
2

Q: //edit I can't find the correct answer so far: When I write

char * const MYCONST1 = "StringString";
printf("Address of MYCONST1: %p\n",MYCONST1);

char * const MYCONST2 = "StringString";
printf("Address of MYCONST2: %p\n",(void*)MYCONST2);

this is the output:

Address of MYCONST1: 0x400b91

Address of MYCONST2: 0x400b91

But they should have different addresses, because the are different constants.


A: Since both the pointers point to same string literal. Compiler optimizes and let them share the same data and hence same address. Try compiling your code with

gcc program_name.c -O 

and see. You will see the addresses different.

Relative: Addresses of two pointers are same

Community
  • 1
  • 1
smRaj
  • 1,246
  • 1
  • 9
  • 13
1

Address of the first character of a C string is in the variable of the string itself, i.e. MYCONST in your case.

Igor Popov
  • 2,588
  • 17
  • 20
1
char * const MYCONST = "StringString";

initializes a pointer MYCONST, making it point to the memory where this string literal is stored.
To print an address of this string, use the pointer's value:

printf("%p\n", (void*) MYCONST);   

instead of

printf("%p\n", (void*) &MYCONST);

which prints the address of pointer itself.

LihO
  • 41,190
  • 11
  • 99
  • 167
1
printf("%p\n",(void*)MYCONST);

Will print the address of the first element of string MYCONST points to.

The reason I didn't put & before MYCONST is because MYCONST is already a pointer.

If you need to print the address of Pointer, then you need to do like &MYCONST.

haccks
  • 104,019
  • 25
  • 176
  • 264
Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
  • No. It will not print the address of`MYCONST`. It will print the address of first character of the string. – haccks Oct 12 '13 at 11:31
  • @haccks yep I made correction. It was a typo. I was trying to answer quickly but other people were faster then me :P – Umer Farooq Oct 12 '13 at 11:34