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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:37:08+00:00 2026-05-16T06:37:08+00:00

Suppose we’ve got two integer and character variables: int adad=12345; char character; Assuming we’re

  • 0

Suppose we’ve got two integer and character variables:

int adad=12345;
char character;

Assuming we’re discussing a platform in which, length of an integer variable is longer than or equal to three bytes, I want to access third byte of this integer and put it in the character variable, with that said I’d write it like this:

character=*((char *)(&adad)+2);

Considering that line of code and the fact that I’m not a compiler or assembly expert, I know a little about addressing modes in assembly and I’m wondering the address of the third byte (or I guess it’s better to say offset of the third byte) here would be within the instructions generated by that line of code themselves, or it’d be in a separate variable whose address (or offset) is within those instructions ?

  • 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-16T06:37:08+00:00Added an answer on May 16, 2026 at 6:37 am

    The best thing to do in situations like this is to try it. Here’s an example program:

    int main(int argc, char **argv)
    {
      int adad=12345;
      volatile char character;
    
      character=*((char *)(&adad)+2);
    
      return 0;
    }
    

    I added the volatile to avoid the assignment line being completely optimized away. Now, here’s what the compiler came up with (for -Oz on my Mac):

    _main:
        pushq   %rbp
        movq    %rsp,%rbp
        movl    $0x00003039,0xf8(%rbp)
        movb    0xfa(%rbp),%al
        movb    %al,0xff(%rbp)
        xorl    %eax,%eax
        leave
        ret
    

    The only three lines that we care about are:

        movl    $0x00003039,0xf8(%rbp)
        movb    0xfa(%rbp),%al
        movb    %al,0xff(%rbp)
    

    The movl is the initialization of adad. Then, as you can see, it reads out the 3rd byte of adad, and stores it back into memory (the volatile is forcing that store back).

    I guess a good question is why does it matter to you what assembly gets generated? For example, just by changing my optimization flag to -O0, the assembly output for the interesting part of the code is:

        movl    $0x00003039,0xf8(%rbp)
        leaq    0xf8(%rbp),%rax
        addq    $0x02,%rax
        movzbl  (%rax),%eax
        movb    %al,0xff(%rbp)
    

    Which is pretty straightforwardly seen as the exact logical operations of your code:

    1. Initialize adad
    2. Take the address of adad
    3. Add 2 to that address
    4. Load one byte by dereferencing the new address
    5. Store one byte into character

    Various optimizations will change the output… if you really need some specific behaviour/addressing mode for some reason, you might have to write the assembly yourself.

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

Sidebar

Related Questions

Suppose you have an array of 1000 random integer numbers and you need to
Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose we have a table A: itemid mark 1 5 2 3 and table
Suppose I have the following CSS rule in my page: body { font-family: Calibri,
Suppose I have a class module clsMyClass with an object as a member variable.
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose a large composite application built on several foundation components packaged in their own
Suppose the following: >>> s = set([1, 2, 3]) How do I get a

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.