Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取C中字符串常量的地址_C_String_Constants - Fatal编程技术网

获取C中字符串常量的地址

获取C中字符串常量的地址,c,string,constants,C,String,Constants,我想得到C中字符串常量的地址 char * const MYCONST = "StringString"; 据我所知,常量“保存”在内存的文本/代码段中。 当我尝试获取MYCONSt中第一个元素的地址时: printf("%p\n",&(MYCONST)); 结果我得到0x7fff15342e28,它在堆栈中,不在文本/代码段中。 有人能帮我找到C中字符串常量的地址吗 //编辑 到目前为止,我还没有找到正确的答案:当我写作的时候 char * const MYCONST1 =

我想得到C中字符串常量的地址

 char * const MYCONST = "StringString";
据我所知,常量“保存”在内存的文本/代码段中。
当我尝试获取MYCONSt中第一个元素的地址时:

 printf("%p\n",&(MYCONST));
结果我得到0x7fff15342e28,它在堆栈中,不在文本/代码段中。 有人能帮我找到C中字符串常量的地址吗

//编辑 到目前为止,我还没有找到正确的答案:当我写作的时候

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

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

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

Address of MYCONST1: 0x400b91

Address of MYCONST2: 0x400b91
MYCONST1的地址:0x400b91

MYCONST2的地址:0x400b91

但是它们应该有不同的地址,因为它们是不同的常量。 当结果的长度为7,而不是0x7fffa5dd398c(类似于区域设置变量)时,有人能解释一下吗

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

谢谢

C字符串的第一个字符的地址在字符串本身的变量中,即您的示例中的MYCONST。

由于MYCONST已经是一个指针,因此您不需要符号。您只需将
%p
转换为
无效*

printf("%p\n",(void*)MYCONST);
使用与,您可以打印
MYCONST
局部变量的地址(您还需要在此处强制转换
void*
,否则地址可能打印不正确),该变量确实位于堆栈上

printf("%p\n",(void *) &MYCONST);
打印
MYCONST
指针变量的地址

printf("%p\n", (void *) MYCONST);
char * const MYCONST = "StringString";
打印
MYCONST
指针变量的值

printf("%p\n", (void *) MYCONST);
char * const MYCONST = "StringString";
初始化指针
MYCONST
,使其指向存储此字符串文字的内存。
要打印此字符串的地址,请使用指针的值:

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

printf("%p\n", (void*) &MYCONST);
它打印指针本身的地址

printf("%p\n",(void*)MYCONST);
将打印字符串
MYCONST
指向的第一个元素的地址

我没有将
&
放在
MYCONST
之前的原因是
MYCONST
已经是指针了


如果您需要打印指针的
地址
,那么您需要像
&MYCONST

Q://编辑我目前找不到正确的答案:当我写的时候

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

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

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

Address of MYCONST1: 0x400b91

Address of MYCONST2: 0x400b91
但是它们应该有不同的地址,因为它们是不同的常量


A:因为两个指针指向相同的字符串文本。编译器进行优化,让它们共享相同的数据和相同的地址。试着用

gcc program_name.c -O 
看看。您将看到不同的地址


相对:

否。它不会打印MYCONST的地址。它将打印字符串第一个字符的地址。@haccks是的,我做了更正。那是个打字错误。我试图快速回答,但其他人比我快:p当我尝试获取指针值的地址时:0x7fff70f54bb8。我认为这不可能是正确的,因为常量在代码段中,而这个地址属于堆栈?