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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:48:26+00:00 2026-06-09T08:48:26+00:00

In other words, may I reinterpret (not convert!) void* pointer as a pointer to

  • 0

In other words, may I reinterpret (not convert!) void* pointer as a pointer to some structure type (assuming that the void* pointer really holds properly converted valid structure address)

Actually I’m interesting in the following scenario:

typedef struct void_struct void_struct_t;

typedef somestruct
{ 
    int member;
    // ... other members ...
}somestruct_t;

union 
{
    void*          pv; 
    void_struct_t* pvs; 
    somestruct_t*  ps; 
}u;

somestruct_t s={};

u.pv= &s;

u.ps->member=1; // (Case 1)  Ok? unspecified? UB? 

u.pvs=(void_struct_t*)&s;

u.ps->member=1;  // (Case 2) )Ok?

What I found in the C11 standard is rather dissapointing for the Case 1:

§6.2.5

28 A pointer to void shall have the same representation and alignment requirements as a
pointer to a character type.[footnote: The same representation and alignment requirements
are meant to imply interchangeability as arguments to functions, return values from
functions, and members of unions.] Similarly, pointers to qualified or unqualified
versions of compatible types shall have the same representation and alignment
requirements. All pointers to structure types shall have the same representation and
alignment requirements as each other. All pointers to union types shall have the same
representation and alignment requirements as each other. Pointers to other types need not
have the same representation or alignment requirements.

It seems, though, that Case 2 is valid, but I’m not 100% sure…

The question is mostly C-oriented, but I’m interesting in C++ too (I’d want the code would be valid while compiling by C++ compiler). Honestly, I found even less in C++11 standard, so even Case 2 seems questionable for me… however, may be I’m missing something.

[edit]
What is the real problem behind this question?

I have a (potentially large) set of types defined as structs.
For each type I need to define a companion type:

typedef struct companion_for_sometype
{
  sometype* p_object;
  // there are also other members
}companion_for_sometype;

Obviously, the companion type would be a template in C++, but I need a solution for C
(more exactly, for "clean C", i.e for intersection of C89 and C++ as I want my code to be also valid C++ code).

Fortunately, it is not a problem even in C, since I can define a macro

DECLARE_COMPANION(type_name) typedef struct companion_for_##type_name
{
  type_name* p_object;
  // there are also other members
}companion_for_##type_name;

and just invoke it for every type that need a companion.

There is also a set of generic operations on companion types.
These operations are also defined by macros (since there are no overloads in pure C).

One of this operations, say

#define op(companion_type_object) blablabla

should assign a void* pointer to p_object field of the companion object,
i.e. should do something like this:

(companion_type_object).p_object= (type_name*) some_function_returning_pvoid(..)

But the macro doesn’t know type_name (only an object of companion type is passed to the macro)
so the macro can’t do the appropriate pointer cast.

The question is actually inspired by this problem.

To solve it, I decide to reinterpret target pointer in the assignment as void* and then assign to it.
It may be done by replacing the pointer in the companion declaration with a union of pointers
(the question is about this case), or one may reinterpret target pointer directly, say:

*(void**) &(companion_type_object).p_object= some_function_returning_pvoid(..)

But I can’t find any solution without reinterpreting pointers (maybe I’m missing some possibilities though)

  • 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-06-09T08:48:27+00:00Added an answer on June 9, 2026 at 8:48 am

    void * is a pointer that can hold any object pointer type, that includes all pointers to structure type. So you can assign any pointer to a structure type to a void *.

    But void * and pointers to structure types are not guaranteed to have the same representation so your case 1 is undefined behavior.

    (C11, 6.2.5p28) “[…] Pointers to other types need not have the same
    representation or alignment requirements.”

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

Sidebar

Related Questions

In other words, can I do something with a volatile variable that could not
i'm not very firm with preg_replace - in other Words i do not really
C language ensures that a pointer to any struct may be converted to void
I have a quetion which may be simple/dumb or not :). In other words
Or, in other words, how to make this work: function foo(){} //do something that
I have an object that may or may not have the properties I want
Assuming I have a cluster of n Erlang nodes, some of which may be
In other words may one use /<tag[^>]*>.*?<\/tag>/ regex to match the tag html element
Is there a Membership.ValidateUser alternative that only checks username password combo? In other words,
In other words, if it returns 200, print True. Else, False. How can I

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.