Will a char array differ in ordering in a little endian or big endian system

前端 未结 4 878
[愿得一人]
[愿得一人] 2021-02-02 03:11

I have an array char c[12] = {\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'0\',\'1\',\'2\',\'3\'} In hexadecimal these values would be {0x61, 0x62, 0x63,

相关标签:
4条回答
  • 2021-02-02 03:47

    The bytes must have ascending memory addresses (as seen by your C program) in the same order that you defined them.

    0 讨论(0)
  • 2021-02-02 03:49

    char arrays are the same on any reasonable system that you will ever encounter. Its the multi byte data types that have differemt ordering. Systems that have odd endianess of the bits existed in the past, but I dont think any of those are made any more.

    0 讨论(0)
  • 2021-02-02 03:54

    The char/byte is, by definition, the smallest addressable unit of memory. Consequently, there is no meaningful concept of "endianness" within a single byte, because the processor cannot index inside it; it can only read the whole thing. Individual bits from within a byte are obtained by mathematical operations, not by addressing; their physical location has nothing to do with how they're read. Endianness only refers to multi-byte objects that have indexable, and thus physically-ordered, subcomponents.

    By definition an array has elements in only one order, otherwise indexing operations would fail, so the machine's notions of endianness for larger types that could hypothetically occupy the same space are not relevant to it either, as they would destroy the array's access rules.

    0 讨论(0)
  • 2021-02-02 03:56

    Think about what "big endian" and "little endian" means.

    Memory is a sequence (or on more interesting machines, several sequences) of bytes. For items that are two or more bytes in size, the bytes of the item can be stored in memory in different orders, and the two most obvious orders are called "big endian" and "little endian" (if you have three or more bytes, then there are more than two possible byte orders, and anyway, who says that the bits of an int for example cannot be distributed over multiple bytes in a chaotic, but fixed order? )

    But for a sequence of bytes, the order of the bytes is the order of the bytes. "Big endian" and "little endian" is defined by the way that the bits of a larger item are arranged in an array of bytes. For a sequence of bytes, they are stored in the order they are stored - there is no possibility of any other order. It's like asking "could the numbers 1, 2, 3, 4 be anything different than the numbers 1, 2, 3, 4? " and of course they can't.

    0 讨论(0)
提交回复
热议问题