Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7931139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:38:07+00:00 2026-06-03T20:38:07+00:00

i.e. if we cast a C or C++ unsigned char array named arr as

  • 0

i.e. if we cast a C or C++ unsigned char array named arr as (unsigned short*)arr and then assign to it, is the result the same independent of machine endianness?

Side note – I saw the discussion on IBM and elsewhere on SO with example:

unsigned char endian[2] = {1, 0};
short x;
x = *(short *) endian;

…stating that the value of x will depend on the layout of endian, and hence the endianness of the machine. That means dereferencing an array is endian-dependent, but what about assigning to it?

*(short*) endian = 1;

Are all future short-casted dereferences then guaranteed to return 1, regardless of endianness?


After reading the responses, I wanted to post some context:

In this struct

struct pix { 
    unsigned char r; 
    unsigned char g;
    unsigned char b; 
    unsigned char a; 
    unsigned char y[2]; 
 }; 

replacing unsigned char y[2] with unsigned short y makes no individual difference, but if I make an array of these structs and put that in another struct, then I’ve noticed that the size of the container struct tends to be higher for the “unsigned short” version, so, since I intend to make a large array, I went with unsigned char[2] to save space overhead. I’m not sure why, but I imagine it’s easier to align the uchar[2] in memory.

Because I need to do a ton of math with that variable y, which is meant to be a single short-length numerical value, I find myself casting to short a lot just to avoid individually accessing the uchar bytes… sort of a fast way to avoid ugly byte-specific math, but then I thought about endianness and whether my math would still be correct if I just cast everything like

*(unsigned short*)this->operator()(x0, y0).y = (ySum >> 2) & 0xFFFF;

…which is a line from a program that averages 4-adjacent-neighbors in a 2-D array, but the point is that I have a bunch of these operations that need to act on the uchar[2] field as a single short, and I’m trying to find the lightest (i.e. without an endian-based if-else statement every time I need to access or assign), endian-independent way of working with the short.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:38:08+00:00Added an answer on June 3, 2026 at 8:38 pm

    Yes, all future dereferences will return 1 as well: As 1 is in range of type short, it will end up in memory unmodified and won’t change behind your back once it’s there.

    However, the code itself violates effective typing: It’s illegal to access an unsigned char[2] as a short, and may raise a SIGBUS if your architecture doesn’t support unaligned access and you’re particularly unlucky.

    However, character-wise access of any object is always legal, and a portable version of your code looks like this:

    short value = 1;
    unsigned char *bytes = (unsigned char *)&value;
    

    How value is stored in memory is of course still implementation-defined, ie you can’t know what the following will print without further knowledge about the architecture:

    assert(sizeof value == 2); // check for size 2 shorts
    printf("%i %i\n", bytes[0], bytes[1]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SWIG converts the below unsigned char array (id) to short[]. On the C side,
I have an unsigned char array that I need in a std::string, but my
Add next changes: Emily::oSeMac^ Terminal; Emily::AsyncSocketController^ _socketManager; delegate void DataArrival(String^, array<unsigned char, 1>^, System::Net::IPEndPoint^);
If you had to convert unsigned short data1[32] to unsigned char* data2 in a
If the array was null-terminated this would be pretty straight forward: unsigned char u_array[4]
Here's my code: struct RS_Token { char id; char cleanup; unsigned char array[sizeof (std::string)
So, I'm trying to convert an array of unsigned char s into an uint32_t
I'm passing an array of 76800 Booleans (could be integers or unsigned char* instead)
Can I cast a pointer to a double as type char * , then
With these variables: NSInteger dataStart; uint64_t dataSize[1]; const unsigned char *beginning; NSInteger bytesEnd; ...at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.