字符*变量地址对的char []变量地址 [英] char * variable address vs. char [] variable address

查看:100
本文介绍了字符*变量地址对的char []变量地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从以下两个声明和初始化打印出地址和字符串:

I am printing out addresses and strings from the following two declarations and initializations:

  char * strPtr = (char *) "This is a string, made on the fly.";
  char charArray [] = "Chars in a char array variable.";

打印时,下面的输出与变量charArray和strPtr完全不同的地址发生。现在的问题是,为什么?

When printed, the following output occurs with wildly different addresses for the variables charArray and strPtr. The question is, "Why?"

印刷:

  printf( "%10s%40s%20p\n", "strPtr", strPtr, &(*strPtr));    
  printf( "%10s%40s%20p\n", "charArray", charArray, charArray);

输出:

    strPtr      This is a string, made on the fly.            0x400880    
 charArray         Chars in a char array variable.      0x7fff12d5ed30

在不同的地址,你看,主要有:0x400880与0x7fff12d5ed30

The different addresses, as you see, are: 0x400880 vs. 0x7fff12d5ed30

这之前声明的变量其余的地址一样,charArray的。

The rest of the variable declared before this have addresses like that of charArray.

同样,问题是,为什么地址如此不同?
感谢您的任何帮助。

Again, the question is, "Why are the addresses so different?" Thanks for any assistance.

推荐答案

由于字符串,例如富巴得到分配在一个不同的地方比你的字符数组。

Because string literals, e.g. "foo bar" get allocated in a "different place" than your char array.

这是的实施依赖的,而是一个典型的实现将会把字符串在你的 .rdata (只读数据)部分您的可执行文件,并且你的字符数组是局部声明,并因此进入堆叠上。

This is implementation dependent, but a typical implementation will put string literals in your .rdata ("read-only data") section of your executable, and your char array is declared locally, and hence goes on the stack.

和不同图像的部分将被映射到完全不同的地址时,他们在RAM中被装载。

And different sections of your image will get mapped to vastly different addresses when they get loaded in RAM.

这篇关于字符*变量地址对的char []变量地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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