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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:19:17+00:00 2026-06-14T13:19:17+00:00

Started by trying to write a small program to translate basic arithmetic into English,

  • 0

Started by trying to write a small program to translate basic arithmetic into English, I end up building a binary tree(which is inevitably very unbalanced) to represent the order of evaluations.
First, I wrote

struct expr;

    typedef struct{
    unsigned char entity_flag;  /*positive when the oprd
    struct represents an entity 
     ---a single digit or a parenthesized block*/                      
    char oprt;

    expr * l_oprd;// these two point to the children nodes 
    expr * r_oprd;
    } expr;

However, to efficiently represent single digits, I prefer

typedef struct{
 unsigned char entity_flag;
 int ival;
} digit;

Since now the “oprd” feild of each “expr” struct may be either of the above struct-s, I now shall modify their types to

void * l_oprd;
void * r_oprd;

Then there comes the “central question”:
how can you access members through a void pointer?
please see the follow code

#include<stdio.h>
#include<stdlib.h>


typedef struct {
int i1;
int i2;} s;
main(){
void* p=malloc(sizeof(s));

//p->i1=1;
//p->i2=2;

*(int*)p=1;
*((int *)p+1)=2;
printf("s{i1:%d, i2: %d}\n",*(int*)p,*((int *)p+1));
}

The compiler wouldn’t accept the commented version!
Do I have to do it with the cluttered approach above?

please help.

PS: as you have noticed ,each struct-s above possess a field of the name “entity_flag”, thus

void * vp;
...(giving some value to vp)
unsigned char flag=vp->entity_flag;

may extract the flag regardless of what void points to, is this allowed in C? or even “safe” in C?

  • 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-14T13:19:19+00:00Added an answer on June 14, 2026 at 1:19 pm

    You can’t access members through void * pointers. There are ways you could cast it (indeed, you don’t even need to state the case explicitly with void *), but even that is the wrong answer.

    The correct answer is to use union:

    typedef union {
      struct{
        unsigned char entity_flag;  /*positive when the oprd
        struct represents an entity 
         ---a single digit or a parenthesized block*/                      
        char oprt;
    
        expr * l_oprd;// these two point to the children nodes 
        expr * r_oprd;
      } expr;
      struct{
        unsigned char entity_flag;
        int ival;
      } digit;
    } expr;
    

    You then access an expression like this (given a variable expr *e):

    e->expr->entity_flag;
    

    And a digit like this:

    e->digit->entity_flag;
    

    Any other solution is a nasty hack, IMO, and most of the casting solutions will risk breaking the “strict aliasing” rules that say that the compiler is allowed to assume that two pointers of different types can’t reference the same memory.


    Edit …

    If you need to be able to inspect the data itself in order to figure out which member of the union is in use, you can.

    Basically, If the top-most fields in two structs are declared the same then they will have the same binary representation. This isn’t just limited to unions, this is true in general across all binaries compiled for that architecture (if you think about it, this is essential for libraries to work).

    In unions it is common to pull those out into a separate struct so that it’s obvious what you’re doing, although it’s not required:

    union {
      struct {
        int ID;
      } base;
      struct {
        int ID;
        char *data
      } A;
      struct {
        int ID;
        int *numeric_data;
      } B;
    }
    

    In this scheme, p->base.ID, p->A.ID, p->B.ID are guaranteed to read the same.

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

Sidebar

Related Questions

I just started to learn Android, I'm trying to write a widget which is
I just started trying to play with OpenCV and I wrote a small program
As an experiment I'm trying to write a generic MVP framework. I started with:
I am currently trying to get a script to write output from other started
I'm just digging a bit into Haskell and I started by trying to compute
I've been trying to write a small library using Thor to help assist me
I am trying to write some unit tests for a small web service written
I've just started to learn Haskell and I am trying to write a simple
Though I've write some small applications with android before, now I trying to write
I'm trying to write some tests with Lettuce , started using lxml but found

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.