John Smithv1
John Smithv1

Reputation: 703

Get address of a string-constant in C

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!

Upvotes: 1

Views: 11741

Answers (6)

smRaj
smRaj

Reputation: 1306

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

Upvotes: 2

Umer Farooq
Umer Farooq

Reputation: 7486

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.

Upvotes: 1

ouah
ouah

Reputation: 145829

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.

Upvotes: 4

LihO
LihO

Reputation: 42083

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.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

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.

Upvotes: 7

Igor Popov
Igor Popov

Reputation: 2620

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

Upvotes: 1

Related Questions