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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:53:51+00:00 2026-05-18T21:53:51+00:00

gcc 4.4.1 c89 I have 2 different structures called origin_t and session_t. I would

  • 0
gcc 4.4.1 c89

I have 2 different structures called origin_t and session_t.

I would to pass the instance of one of these structure to my function. However, before I can perform an operation on these I need to cast it to the correct type. My problem is that I don’t know how to check for the correct type. Is there any standard c function that can check for the correct instance of this structure.

Many thanks for any advice,

const char* get_value(void *obj)
{
    /* Cast to the correct structure type */    
    if(obj == origin) {
        /* Is a origin structure */
        origin_t *origin = (origin_t*)obj;
    } 
    else if(obj == session) {
        /* Is a session structure */
        session_t *session = (session_t*)obj;
    }
}
  • 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-18T21:53:51+00:00Added an answer on May 18, 2026 at 9:53 pm

    The best way would be to combine the types (if possible!) under a common type such as:

    typedef enum {
        t_origin,
        t_session
    } type_t;
    
    struct base_t {
        type_t type;
        union {
            origin_t origin;
            session_t session;
        };
    }
    

    and then:

    const char* get_value(base_t *obj)
    {
        /* Cast to the correct structure type */    
        if(obj->type == t_origin) {
            /* Is a origin structure */
            origin_t *origin = &obj->origin;
        } 
        else if(obj->type == t_session) {
            /* Is a session structure */
            session_t *session = &obj->session;
        }
    }
    

    There are no ways of determining types from a pointer in C. You have to add your own type mechanism. This is one way to do it without being intrusive in the subtypes (origin_t and session_t). You also don’t have to do weird casts, and compromise the already weak type system in the C language.

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

Sidebar

Related Questions

gcc 4.4.2 c89 I have a file called main.c. I want the result of
gcc 4.4.4 c89 I have the following function but I cannot free the memory.
gcc 4.4.3 c89 I have the following source file. However, I get a stack
gcc 4.4.2 c89 I have the following code. #if defined ( __linux__ ) log_msg(stderr,
gcc 4.4.4 c89 I have seen this in some code I am maintaining. #define
gcc 4.4.2 c89 I have this code snippet that I have to repeat in
gcc 4.4.3 c89 I have some functions that initialize some hardware and return either
gcc 4.4.4 c89 I have always thought of using malloc for the life of
gcc 4.4.3 c89 linux I am using log4c and have placed the inc and
gcc 4.4.4 c89 What is better to convert a string to an integer value.

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.