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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:54:15+00:00 2026-05-13T12:54:15+00:00

The Code used #include<stdio.h> struct st { char a; short c; int b; };

  • 0

The Code used

#include<stdio.h>

struct st
{
 char a;
 short c;
 int b;
};

struct st s1;
int main()
{
        printf("%p %p \n",(&s1.b)-1, &s1);
}

If I print the address of &s1.b it prints 0x804a01c and &s1.b-2 prints 0x804a018
why it is printing same address 0x804a01c if i select &s1.b-1 ?

  • 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-13T12:54:16+00:00Added an answer on May 13, 2026 at 12:54 pm

    Thank you for posting your code. Now I see the issue. It is because of padding. To wit:

    printf("sizeof(char): %d\n", sizeof(char));
    printf("sizeof(short): %d\n", sizeof(short));
    printf("sizeof(int): %d\n", sizeof(int));
    printf("sizeof(struct st): %d\n", sizeof(struct st));
    

    On my machine this prints

    1
    2
    4
    8
    

    You might think, shouldn’t sizeof(struct st) be 1 + 2 + 4 = 7? That’s certainly a reasonable thought but because of alignment issues there is padding between a and c. Therefore, in memory, the struct looks like the following (relative to the first byte of the struct):

    0x00000000: char                 a
    0x00000001: padding
    0x00000002: first byte of short  c
    0x00000003: second byte of short c
    0x00000004: first byte of int    b
    0x00000005: second byte of int   b
    0x00000006: third byte of int    b
    0x00000007: fourth byte of int   b
    

    Consequently (relative to &s1):

    &s1.b - 1 is ((long)&s1.b) - sizeof(int) = 4 - 4 = 0 = &s1
    

    This is why both &s1 and &s1.b - 1 will print the same address. In particular if

    &s1 = 0x804a01c
    

    then

    &s1.b = 0x804a01c + 0x00000004 = 0x804a020
    

    and

    &s1.b - 1 = 0x804a020 - 0x00000004 = 0x804a01c
    

    and

    &s1.b - 2 = 0x804a020 - 0x00000008 = 0x804a018
    

    Note, finally, that this is implementation-specific behavior. This is not portable!

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

Sidebar

Related Questions

In our code we used to have something like this: *(controller->bigstruct) = ( struct
Is there a way to find the number of lines of code used in
I'm currently refactoring/tidying up some old C code used in a C++ project, and
Here is the code currently used. public String getStringFromDoc(org.w3c.dom.Document doc) { try { DOMSource
I've seen (and used) code to have a link spawn a javascript action many
Info - for better formatting, I used code-formatting throughout the whole posting. Hi, I
I used the code below to compress files and they keep growing instead of
Which programming language(s) were used to code Windows Vista?
As per my understanding stateless session beans are used to code the business logic.
I used the following piece of code in the service to debug the service

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.