简体   繁体   English

通过其地址访问C中的位字段

[英]Accessing bit-field in C by its address

What is the reason behind not allowing to access a bit field in C using its address, is it cause it might not be an address that is not system word aligned ..? 不允许使用其地址访问C中的位字段的原因是什么,是因为它可能不是一个非系统字对齐的地址..? or as it doesn't make sense to get bit's address within a byte...?(cause this types pointer arithmetic will be awkward ?) 或者因为在一个字节内得到位的地址没有意义......?(因为这种类型的指针运算会很尴尬吗?)

Bits do not have addresses. 比特没有地址。 That's why you can't refer to them by address. 这就是为什么你不能通过地址来引用它们。 The granularity of addressing is the char . 寻址的粒度是char

I guess the reasoning is that the language was design to match the architecture it targeted, and I know of no machine which allows addressing of individual bits. 我猜测的原因是该语言的设计与其所针对的架构相匹配,而且我知道没有机器允许对各个位进行寻址。

The smallest unit of addressable memory in C is a char , because this corresponds to the smallest unit of addressable memory on most CPU architectures. C中可寻址内存的最小单位是char ,因为这对应于大多数CPU架构中可寻址内存的最小单位。 * It doesn't make sense to talk about the address of a bit. *谈论一点地址是没有意义的。


* One could imagine a hypothetical machine that allowed addressing of individual bits, but it would be pretty esoteric. *可以想象一个假设的机器允许寻址各个位,但它会非常深奥。

In c smallest addressable unit of memory is considered a Byte. 在c中,最小可寻址单元的存储器被认为是字节。 A pointer points to a memory location which can be of any data_type (a pointer is also another variable). 指针指向一个可以是任何data_type的内存位置(指针也是另一个变量)。 Bits receding in byte don't have any address , rather they do have a bit position. 以字节为后退的位没有任何地址,而是具有位位置。

So basically you can not point to particular bit , you can point to a byte or whole word. 所以基本上你不能指向特定位,你可以指向一个字节或整个字。

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

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