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

  • Home
  • SEARCH
  • 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 6993803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:48:40+00:00 2026-05-27T19:48:40+00:00

I am watching Jerry Cain’s Programming Paradigms Lecture 3 video where the effect of

  • 0

I am watching Jerry Cain’s Programming Paradigms Lecture 3 video where the effect of an element assignment after casting between an int array and short array is demonstrated. Essentially the argument is that if you were to assign an int array element arr[3] = 128, then temporarily cast the int array to a short* and assign arr[6] = 2, then arr[3] should become 128 + 512 = 640 because the 2 would be interpreted as being in the 2^9th position. Code to demonstrate:

#include <stdio.h>

int main() {
  printf("sizeof(int) is %lu\n", sizeof(int));
  printf("sizeof(short) is %lu\n", sizeof(short));
  int arr[5];
  arr[3] = 128;
  ((short*)arr)[6] = 2;
  printf("arr[3] is equal to %d\n", arr[3]); //expect 640, get 2 instead
  return 0;
}

When I run this code though, I get the following output:

sizeof(int) is 4
sizeof(short) is 2
arr[3] is equal to 2

I expect arr[3] to be equal to 640, but instead it is simply equal to 2. I am admittedly a C noob – can anyone explain?

  • 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-27T19:48:40+00:00Added an answer on May 27, 2026 at 7:48 pm

    Big-endian vs little-endian, I think.

    The code is inherently platform-specific (officially, almost certainly undefined behaviour). I’m not sure you should be being taught it, but that is, I guess, an issue for another time.

    The 2 is assigned to two of the four bytes of arr[3]. If you assigned to ((short *)arr)[7] instead, you might see the expected result.

    What machine are you testing on (what type of CPU)?


    On second thoughts – although part of the issue is perhaps big-endian vs little-endian, the other problem is short vs char. Here’s some more code that shows various pathways to the solution:

    #include <stdio.h>
    
    int main(void)
    {
        printf("sizeof(int) is %lu\n", sizeof(int));
        printf("sizeof(short) is %lu\n", sizeof(short));
        int arr[5];
    
        arr[3] = 128;
        ((short*)arr)[6] = 2;
        printf("arr[3] is equal to %8d (0x%08X)\n", arr[3], arr[3]);
    
        arr[3] = 128;
        ((short*)arr)[7] = 2;
        printf("arr[3] is equal to %8d (0x%08X)\n", arr[3], arr[3]);
    
        for (int i = 12; i < 16; i++)
        {
            arr[3] = 128;
            ((char *)arr)[i] = 2;
            printf("arr[3] is equal to %8d (0x%08X) i = %d\n", arr[3], arr[3], i);
        }
    
        return 0;
    }
    

    The output of this revised code is:

    sizeof(int) is 4
    sizeof(short) is 2
    arr[3] is equal to        2 (0x00000002)
    arr[3] is equal to   131200 (0x00020080)
    arr[3] is equal to        2 (0x00000002) i = 12
    arr[3] is equal to      640 (0x00000280) i = 13
    arr[3] is equal to   131200 (0x00020080) i = 14
    arr[3] is equal to 33554560 (0x02000080) i = 15
    

    Testing on MacOS X 10.7.2 with GCC 4.2.1 XCode 4.2 (LLVM).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After watching the very known video on this topic I decided to go with
After watching this video , I am wondering if I am using my controllers
After watching the latest hanselminutes on 9 video, I went to our dev environment
After watching the Knockout.JS video with Steve Sanderson I decided this would be great
After watching the Pragmatic Studio screen casts on Erlang, the last video on Supervisors
After watching the WWDC video on the accelerate framework, I saw that it had
I'm watching some lecture's on Functional Programming and the main 'data structure' so to
After watching Channel 9's video on F# Type Providers, I'm wondering about data schema
After watching this video , I started thinking about how I could implement something
After watching The Dark Knight I became rather enthralled with the concept of the

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.