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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:40:26+00:00 2026-05-15T13:40:26+00:00

Here is an interview question that I saw on some forum. I’ve been trying

  • 0

Here is an interview question that I saw on some forum. I’ve been trying to figure out how it works but I don’t quite get it. Could somebody explain how it works?

Q: Given a pointer to member a within a struct, write a routine that returns a pointer to the struct.

struct s 
{
   ...
   int a;
   …
};

struct s *get_s_ptr(int *a_ptr)
{
   // implement this.
}

The answer is:

struct s* get_s_ptr(int *a_ptr)
{
   return (struct s*)((char*)a_ptr - (int)&((struct s*)0)->a);
}
  • 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-15T13:40:27+00:00Added an answer on May 15, 2026 at 1:40 pm

    How does it work?

    The fundamental equation here (all arithmetic in bytes) is

    address of struct member s->a == s + byte offset of a
    

    Given the type of s, a single compiler, and a single target machine, they determined the byte offset of a—it’s the same for every struct of type s.

    You’re given the left-hand side and your interviewer asked you to recover s. You can do this by getting a new equation; subtract the byte offset from both sides:

    address of struct member s->a - byte offset of a == s
    

    In the problem, you’re given the address of s->a, but you have to figure out the byte offset. To do this you use the original equation again with s set to zero:

    address of struct member s->a where s is zero == zero + byte offset of a 
                                                  == byte offset of a
    

    The left-hand side in C is built as follows

    struct pointer s where s is zero                            (struct s *)0
    struct member s->a where s is zero                          ((struct s*)0)->a
    address of s->a where s is zero                             &((struct s*)0)->a
    

    Final steps:

    1. To make the arithmetic legal C this byte offset is cast to an integer.
    2. To make sure the subtraction is done in units of bytes, a_ptr is cast to char *.
    3. To give the result the right type the difference is cast to struct s *.

    Addendum: As Eli Bendersky points out, you should try to avoid situations where this code would be necessary. There is almost always a better way.

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

Sidebar

Related Questions

I am trying to code a trival interview question of reversing a string. This
I'm getting an error here that says I haven't defined a method, but it
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here is the scenario: I'm writing an app that will watch for any changes
As an exercise (read:interview question) in index optimisation, I need a query which is
Had the following as an interview question a while ago and choked so bad
this is an interview question my friend was asked yesterday. The question was something

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.