Accessing bit-field in C by its address

倖福魔咒の 提交于 2019-12-19 04:20:11

问题


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 ..? or as it doesn't make sense to get bit's address within a byte...?(cause this types pointer arithmetic will be awkward ?)


回答1:


Bits do not have addresses. That's why you can't refer to them by address. The granularity of addressing is the 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.




回答2:


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.* 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.


回答3:


In c smallest addressable unit of memory is considered a Byte. A pointer points to a memory location which can be of any data_type (a pointer is also another variable). 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.



来源:https://stackoverflow.com/questions/8258483/accessing-bit-field-in-c-by-its-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!