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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:02:06+00:00 2026-05-29T09:02:06+00:00

typedef struct { int data1; float data2; } packetType1; typedef struct { bool data1;

  • 0
typedef struct
{
    int data1;
    float data2;
} packetType1;

typedef struct
{
     bool data1;
} packetType2;

typedef union
{
     packetType1 p1;
     packetType2 p2;
} packet;

struct
{
    int type;
    packet myPacket;
} message;

Now I make a message and pass the pointer to this message to a function. Inside this function I need to de-reference the message and take out the necessary data from it.

This data will depend on whether packet was filled with data of packetType1 type or of packetType2 type. Inside message , the integer variable type can contain the value 1 or 2 only, telling that packet inside message is of type packetType1 or of type packetType2.

I want to know if this is safe to do –

packetType1 s1;
s1.data1 = 10;
s1.data2 = 22.22;


packetType2 s2;
s2.data1 = true; 

packet pack1; 
pack1.p1 = s1;

packet pack2;
pack2.p2 = s2;

message m1;
m1.type = 1;
m1.myPacket = pack1;

message m2;
m2.type = 2;
m2.myPacket = pack2;

eatPacket( &m1 );
eatPacket( &m2 );

void eatPacket( void *p )
{

    if( *(int*)p == 1)
    {
       message msg = *(message*)p
       cout << msg.myPacket.data1;
       cout << msg.myPacket.data2;
    }

    else if( *(int*)p == 2)
    {
       message msg = *(message*)p
       cout << msg.myPacket.data1;           
    }

}

Edit: ( For all those who are asking why I had used void* )

These messages are sent from one process to other using posix message queue and then decoded there. Problem is that even this message structure could be different. Only thing I am sure is that the variable int type will always be there to guide me. Other part of the message might change. So I had to make this function generic by making it accept a void * and then do decoding internally using the value provided by variable type.

Consider that someone makes a message like this now-

struct
{
    int type;

    float data;
    bool moreData;
    int evenMoreData;

} newMessage;

For this new message it was decided that value for variable type would always be 3.

So in my eat function I will just add another clause like this

if( *(int*)p == 3)
{
       newMessage msg = *(newMessage*)p
       cout << msg.data;
       cout << msg.moreData;
       cout << msg.evenMoreData;
}

Will it still be safe to do so ? I hope this makes sense now ?

  • 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-29T09:02:07+00:00Added an answer on May 29, 2026 at 9:02 am

    It looks fine, but I’d rewrite eatPacket() like this:

    void eatPacket(const message& msg)
    {
    
        if(msg.type == 1)
        {
           cout << msg.myPacket.data1;
           cout << msg.myPacket.data2;
        }
    
        else if(msg.type == 2)
        {
           cout << msg.myPacket.data1;           
        }
    
    }
    

    There’s really no need for the void* gymnastics that I can see. If you really need msg to be a pointer you can modify the above in a straightforward way (-> for ., etc).

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

Sidebar

Related Questions

When I make my own struct, say: struct myStruct{ int data1; int data2; string
I've got something like: typedef struct Data_s { int field1; int field2; } Data;
Consider the following: typedef struct { int a; int b; int c; int d;
I am curious why this code works: typedef struct test_struct { int id; }
Let's say I have a first structure like this: typedef struct { int ivalue;
Given a struct, e.g. typedef struct { int value; } TestStruct; Why does the
This is my c++ struct (Use Multi-Byte Character Set) typedef struct hookCONFIG { int
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
Ok so I have struct like this typedef struct { float x; float y;
Declaring a struct with typedef typedef struct some_struct { int someValue; } *pSomeStruct; and

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.