Get address of a string-constant in C

爷,独闯天下 提交于 2019-12-13 23:03:39

问题


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!


回答1:


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




回答2:


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.




回答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.




回答4:


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




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/19333602/get-address-of-a-string-constant-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!