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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:13:48+00:00 2026-05-13T21:13:48+00:00

The whole code is written in ANSI C, and it should remain so. I

  • 0

The whole code is written in ANSI C, and it should remain so.
I have a callback defined like so:

typedef enum {
    Event_One,
    Event_Two,
    Event_State
} EventEnum;

typedef void (*callback)(EventEnum event, void* data);

The callback recipient interprets data depending on the event value. This is the contract between the components. Sometimes it is a pointer to the structure, sometimes it might be a string, other cases might be other data. I am defining an additional event and setting up a new “contract” that data is an enumeration. Like so:

typedef enum {
    State_Initial = 0,
    State_Running,
    State_Final
} StateEnum;

Then somewhere in the code I have a callback function, which is doing this

void ProcessEvent (EventEnum event, void* data)
{
    if (event == Event_State)
    {
         StateEnum state = (StateEnum)data; /* <<<<<<<<<<< */
         switch (state) {
         case State_Initial:
             <...>
             break;
         case State_Running:
             <...>
             break;
         case State_Final:
             <...>
             break;
         }
    }
}

The callback above is called like so:

{
    callback infoCallback = ProcessEvent; /* This is only for example,
                                             done during initialization */
    <...>
    StateEnum someState = State_Running;
    <...>
    infoCallback(Event_State, (void*)someState); /* <<<<<<<<<<<<<<<<<<< */
}

Is there anything fundamentally wrong with typecasting void* to StateEnum and vice versa? What are the possible gotchas in this way? Any thoughts on testability and maintainability?

EDIT : The code compiles, links and runs OK right now. I want to know why this should not be done and if there are any real reasons why the code must be changed.

  • 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-13T21:13:48+00:00Added an answer on May 13, 2026 at 9:13 pm

    Only pointers to objects (i.e., not functions) can be converted to void * and back. You can’t convert a non-pointer to void * and back. So, change your call to:

    infoCallback(Event_State, &someState);
    

    And your function to:

    StateEnum *state = data;
    switch (*state)
    ...
    

    From the standard (6.3.2.3):

    An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

    Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

    So, what you are doing is implementation-defined. If your implementation defines it to be OK to convert int to a pointer and back, then the code will work. In general, it is not portable. For more details, see this thread on comp.lang.c.

    C99 additionally defines types intptr_t and uintptr_t, which are integral types, and it is guaranteed to convert a void * to them and back.

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

Sidebar

Related Questions

I have an updatepanel which doesn't work. My whole code: <%@ Page Language=C# MasterPageFile=~/Master.Master
I have a whole heap of legacy code that I checked into my SVN
I have written some code to ensure that items on an order are all
I have written my own implementation of java.utils.List. Now I'd like to test it,
I have been assigned a project with a lot of poorly written code that
I have written some code which sends queries to google and returns the query
before posting the whole code, i wanted to make sure I am not missing
I realize that a whole lot of code cannot fit here, but I am
Info - for better formatting, I used code-formatting throughout the whole posting. Hi, I
I have heard of desktop applications whose code has been open-sourced, but are there

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.