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 7569819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:15:42+00:00 2026-05-30T15:15:42+00:00

unsigned char myArray[10]; myArray[0] = 0; myArray[1] = 0; myArray[2] = 0; myArray[3] =

  • 0
unsigned char myArray[10];

myArray[0] = 0;
myArray[1] = 0;
myArray[2] = 0;
myArray[3] = 22;

cout << *((int *) (myArray)) << endl;

I was shown this code in a class, but it was not explained very well. I have determined that the output is equal to (256^3)*22, but I don’t know why this is what it prints out. Can anyone explain this to me?

  • 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-05-30T15:15:44+00:00Added an answer on May 30, 2026 at 3:15 pm

    That code is unsafe and non-portable.

    It (obviously) stores the values 0, 0, 0, and 22 in the first 4 elements of myArray, each of which is an unsigned char (a single byte).

    The expression myArray is the name of an array, which in most contexts (including this one) “decays” to a pointer to the array’s first element, of type unsigned char*. The cast converts the unsigned char* to int*, and the * operator defererences that pointer, yielding an int result.

    So *((int *) (myArray)) takes the first 4 bytes of myArray and reinterprets them as an int object.

    There are at least 3 problems with this.

    1. There is no guarantee that myArray is correctly aligned for an int object. It probably is, but if it isn’t the behavior is undefined; you could get a bus error, or an incorrect result, or something else altogether. (x86 processors aren’t very picky about alignment, but other systems are.)
    2. There is no guarantee that sizeof (int) == 4. If int is 8 bytes, then it will reinterpret the first 8 bytes of myArray as an int — and 4 of those bytes are garbage.
    3. The representation of int can vary. The main problem is byte order. Assuming nothing else goes wrong, the result could be either 2563 * 22, or 22 — or even something else.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
Suppose this code: unsigned char list[3] = { 1, 2, 3 }; struct _struct{
i have written this structure: struct bmpheader { unsigned char magic[2]; unsigned int fsize;
I have this code: string get_md5sum(unsigned char* md) { char buf[MD5_DIGEST_LENGTH + MD5_DIGEST_LENGTH]; char
I have a small sample function: #define VALUE 0 int test(unsigned char x) {
What does the following C++ code mean? unsigned char a : 1; unsigned char
I have an unsigned char array that I need in a std::string, but my
In C++ given an array like this: unsigned char bogus1[] = { 0x2e, 0x2e,
/* ... */ private: unsigned char** a; /* ... */ void foo::init_a() { this->a
I need to store large amounts of unsigned char s and/or int s (potentially

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.