简体   繁体   English

理解'&'运算符

[英]Understanding the '&' operator

As of what I know about '&' operator, it returns the base address of the operand in memory. 根据我对'&'运算符的了解,它返回内存中操作数的基址。

Let us imagine the following scenario (as on my machine): 让我们想象下面的场景(就像在我的机器上一样):

  • sizeof(int) = 4 bytes sizeof(int)= 4个字节
  • sizeof(float) = 4 bytes sizeof(float)= 4个字节
  • sizeof(char) = 1 byte sizeof(char)= 1个字节

Now, if I write something like this: 现在,如果我写这样的东西:

void main() {
 int i = 5411;
 int *ip = &i;
 char *c = &i;

 printf("%d",*ip);
 printf("%c",*c);
}

The first printf() should give me 5411. Talking about the second printf(), the base address of i contains 10101001 (higher order 8 bits = 1 byte for char type pointer). 第一个printf()应该给我5411.谈到第二个printf(),i的基地址包含10101001(高阶8位= 1字节用于char类型指针)。 Hence *c should give me 169, which when converted to %c is an invalid character. 因此* c应该给我169,转换为%c时是无效字符。

But the compiler is giving me '#' or some other valid output. 但编译器给了我'#'或其他一些有效的输出。 Why is it so ? 为什么会这样? Any inputs ? 有什么投入?

EDIT (taken from the author's comment on one of the answers): 编辑 (取自作者对其中一个答案的评论):

That was just a dummy case, since I was away from the actual machine. 那只是一个虚拟案例,因为我离开了实际的机器。
The actual case is i = 5411 实际情况是i = 5411

You seems to have trouble understanding how integers are stored in memory. 您似乎无法理解整数如何存储在内存中。 Take 5411 as example. 以5411为例。

5411 = 1010100100011

this number 13 binary digits has however, since an int is 32-bit, it must be pad to 32 digits 但是,这个13位二进制数字,因为int是32位,所以它必须填充到32位

5411 = 00000000 00000000 00010101 00100011

On a little endian machine (x86, ARM by default), the least significant bytes are stored in the front, so in the memory: 在一个小端机器上(默认情况下为x86,ARM),最低有效字节存储在前面,因此在内存中:

00100011   00010101    00000000    00000000
^
c          c + 1       c + 2       c + 3
ip

Therefore, *c should return 00100011 ie 35 ( '#' ). 因此, *c应返回00100011,即35( '#' )。

ASCII only defines characters up to 127. Besides that, what you really want to do is print the numeric corresponding to the value in *c , this is also done using %d ... ASCII只定义了最多127个字符。除此之外,你真正想做的是打印与*c的值对应的数字,这也是使用%d来完成的...

 printf("%d",*c);

...should display the number as you expect. ...应该显示您期望的数字。

Firstly, your program is ill-formed. 首先,你的程序是不正确的。 Neither C nor C++ allows initializing an char * pointer with an int * value. C和C ++都不允许使用int *值初始化char *指针。 You need an explicit cast in your initialization of c pointer. 在初始化c指针时需要显式转换。

Secondly, which byte of the original integer i - higher order or lower order - resides at its "base address" is implementation-defined. 其次,原始整数i哪个字节 - 更高阶或更低阶 - 驻留在其“基地址”是实现定义的。 There are little-endian architectures, where the lower-order but will be seen through *c (which is has value 130 on a 8-bit char machine, not 114 ). 有小端架构,其中较低阶但通过*c (在8位char机器上具有值130 ,而不是114 )可以看到。 And there are big-endian architectures, where the higher-order but will be seen through *c (which is 0 on a 8-bit char machine). 并且有大端架构,其中高阶但通过*c (在8位char机器上为0可以看到。 So you should expect either character with code 130 or character with code 0 to be printed with %c format specifier. 因此,您应该期望使用代码130字符或使用代码0字符使用%c格式说明符进行打印。

Thirdly, in a typical implementation there's normally no such thing as "invalid character code". 第三,在典型的实现中,通常没有诸如“无效字符代码”之类的东西。 For any code something will usually be printed in one way or the other. 对于任何代码,通常会以某种方式打印某些代码。 I don't see though how you managed to obtain # as the output from your code. 我不知道你是如何设法获得#作为代码的输出。 Is this the real code you were running? 这是你运行的真实代码吗?

The Address of *c is that of i, because you have assigned c to &i. * c的地址是i的地址,因为您已将c分配给&i。 It will then take the highest or lowest (depends on the endian) and print that character. 然后它将取最高或最低(取决于endian)并打印该字符。

Just to learn something about the encoding of your integers you should experiment a bit and do 只是为了了解整数的编码,你应该尝试一下并做

printf("0x%X, %X|%X|%X|%X\n", 
  i, 
  i & 0xFF,
  (i >> 8) & 0xFF
  (i >> 16) & 0xFF
  (i >> 24) & 0xFF
  );

An then do the same with c[0] , c[1] etc and other format strings as %c . 然后使用c[0]c[1]等以及其他格式字符串作为%c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM