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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:47:09+00:00 2026-05-17T06:47:09+00:00

If I have structure definitions, for example, like these: struct Base { int foo;

  • 0

If I have structure definitions, for example, like these:

struct Base {
  int foo;
};

struct Derived {
  int foo; // int foo is common for both definitions
  char *bar;
};

Can I do something like this?

void foobar(void *ptr) {
  ((struct Base *)ptr)->foo = 1;
}

struct Derived s;

foobar(&s);

In other words, can I cast the void pointer to Base * to access its foo member when its type is actually Derived *?

  • 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-17T06:47:10+00:00Added an answer on May 17, 2026 at 6:47 am

    Many real-world C programs assume the construct you show is safe, and there is an interpretation of the C standard (specifically, of the “common initial sequence” rule, C99 §6.5.2.3 p5) under which it is conforming. Unfortunately, in the five years since I originally answered this question, all the compilers I can easily get at (viz. GCC and Clang) have converged on a different, narrower interpretation of the common initial sequence rule, under which the construct you show provokes undefined behavior. Concretely, experiment with this program:

    #include <stdio.h>
    #include <string.h>
    
    typedef struct A { int x; int y; }          A;
    typedef struct B { int x; int y; float z; } B;
    typedef struct C { A a;          float z; } C;
    
    int testAB(A *a, B *b)
    {
      b->x = 1;
      a->x = 2;
      return b->x;
    }
    
    int testAC(A *a, C *c)
    {
      c->a.x = 1;
      a->x = 2;
      return c->a.x;
    }
    
    int main(void)
    {
      B bee;
      C cee;
      int r;
    
      memset(&bee, 0, sizeof bee);
      memset(&cee, 0, sizeof cee);
    
      r = testAB((A *)&bee, &bee);
      printf("testAB: r=%d bee.x=%d\n", r, bee.x);
    
      r = testAC(&cee.a, &cee);
      printf("testAC: r=%d cee.x=%d\n", r, cee.a.x);
    
      return 0;
    }
    

    When compiling with optimization enabled (and without -fno-strict-aliasing), both GCC and Clang will assume that the two pointer arguments to testAB cannot point to the same object, so I get output like

    testAB: r=1 bee.x=2
    testAC: r=2 cee.x=2
    

    They do not make that assumption for testAC, but — having previously been under the impression that testAB was required to be compiled as if its two arguments could point to the same object — I am no longer confident enough in my own understanding of the standard to say whether or not that is guaranteed to keep working.

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

Sidebar

Related Questions

I have a structure like this struct structure { BaseObject &A; //BaseObject has a
I have a structure: typedef struct stock { const char* key1p2; // stock code
I have structure like this. <ul class=gallerylist> <li> <image ...> <input ...> </li> <li>
I have folder structure and I would like to create JSON objects based on
I have a structure like this: var var1 = { array1 : ['value1','value2', ...],
I have an XML file with the following structure: <Definitions> <Definition Execution=Firstkey Name=Somevalue></Definition> <Definition
I have a web page with basicly the following URL structure: www.example.com/main.php?userId=mattias.wolff www.example.com/definitions.php?userId=mattias.wolff www.example.com/tasks.php?userId=mattias.wolff
I have menu structure as show here . All item have a background img.
I have below structure <div class=mybox> <div id=imageslide> <a href=><img src=url /></a> </div> </div>
I have this structure: Activity A has a ViewPager. The pages of a ViewPager

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.