难道我们能够打印出的地址字符串? [英] Are we able to print out the address for string literal?

查看:117
本文介绍了难道我们能够打印出的地址字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个谷歌,我意识到,有人说可以
有人说,它不能。

我只是想验证。

例如

 字符gchararr1i [] =试123; //全球字符数组初始化
 字符* gptr1i =你好,* gptr2ui; //全球字符指针初始化和未初始化
 浮gfloati = 123.4; //全球浮动变种的init
 双gdoubleui; //全球双VAR UN-INT
 INT主(INT ARGC,CHAR *的argv []){   的printf(字符串文字\\ n地址\\试123 \\=%p和\\你好\\=%P \\ N,gchararr1i,gptr1i); //输出字符串测试123和地址你好   F1(10,-20,30.3,'A',45.67);
   出口(0);
}


解决方案

您可以采取一个字符串的地址,但你不能肯定的是完全相同的内容,两个字符串文字将在同一个地址(或者,他们将不会有相同的地址)。编译器工具链是可以自由地做,因为它在这方面的意愿。

该标准,甚至允许一个字符串与另一个共享内存。例如世界可能的地址点到文字的Hello World。我不知道这是否实现的,但我还没有检查它的。

I did a google on that, and I realized that some say it can some say it cannot.

I would just like to verify.

For example

 char gchararr1i[] = "testing 123"; // global char array init
 char *gptr1i = "hello", *gptr2ui; // global char pointer init and un-init
 float gfloati = 123.4; // global float var init
 double gdoubleui; // global double var un-int


 int main(int argc, char *argv[]) {

   printf("\n Address of  string literals \"testing 123\" = %p and \"hello\" = %p \n", gchararr1i, gptr1i);// output the addresses of string literals "testing 123" and "hello" 

   f1(10, -20, 30.3, 'A', 45.67);
   exit(0);               
}

解决方案

You can take the address of a string literal, but you can't be certain that two string literals with the exact same contents will be at the same address (or that they won't have the same address). The compiler toolchain is free to do as it wishes in that regard.

The standard even permits one string literal to share memory with another. For example the address of "world" might 'point into' the literal "hello world". I'm not aware of an implementation that does this, but I haven't checked for it either.

这篇关于难道我们能够打印出的地址字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆