Thread: Detect byte order using array

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    189

    Detect byte order using array

    Hello I try to dectect byte order "Endianess" using arrays.
    If we define an array of 2 elements, in little endian element 0 shall be stored first simple comarsion of address of element 0 and 1 should give the answer. However element 1 will always have higher addresss.

    Code:
    char array[] = {'A','B'};
    if (&(array[1]) > &(array[0]))
    {
        printf("Little Endian \n");
    }
    Does it make any sense?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,538
    No, that just tells you that arrays are stored in incremental addresses.
    Question 20.9
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,084
    That's a huge misunderstanding of big/little endian. It has absolutely nothing to do with chars (i.e., bytes (usually!)). It is only for multi-byte values. Which order are those multiple bytes stored in? That's the question.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  4. #4
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    189
    @Salem, thanks! Very informative as always
    @john.c, it's not misunderstanding - after all an array of two chars is two byte value right? So I expected it to be addressed and stored in memory accoriding to some endianess. So why arrays don't obay endianess?

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,084
    Oh, my mistake, I thought it was a misunderstanding but apparently you understand perfectly. Good luck with that.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. long integers and byte order
    By sonnichs in forum C Programming
    Replies: 19
    Last Post: 08-12-2011, 01:01 PM
  2. Swapping byte order, aka Big endian?
    By Alexlf in forum C Programming
    Replies: 7
    Last Post: 01-15-2011, 10:44 AM
  3. Binary File - Byte order / endian
    By chico1st in forum C Programming
    Replies: 27
    Last Post: 08-22-2007, 07:13 PM
  4. byte order change
    By onebrother in forum C Programming
    Replies: 1
    Last Post: 08-06-2007, 05:40 AM
  5. byte and bit order question
    By chunlee in forum C Programming
    Replies: 7
    Last Post: 11-07-2004, 01:50 PM

Tags for this Thread