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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:27:03+00:00 2026-05-26T00:27:03+00:00

I have a question about union in c. The following code is the server

  • 0

I have a question about union in c. The following code is the server side, we receive Signal2 from the client side, in the service function, can I check the signal type like this: data_p->header.type? and in the switch case, how could I pass the parameter to function printInfo(), the parameter of it is the pointer to the union, how to pass? And the last question is the pointer to the union, point to what? and what is inside of the union, when these three structs are allocated, how could they fit into the union(with the size of the large struct)?

   union
    {
        Header  header;
        Signal1 signal1;
        Signal2 signal2;
    } Data;

struct Header 
{   
    int type:
    int protocol;
    int clientId;
    int serverId;
};


struct Signal1
{
    int type:
    int protocol;
    int clientId;
    int serverId;
    char[80] something;
};


struct Signal2
{
    int type:
    int protocol;
    int clientId;
    int serverId;
    int[100] something;
};


void service(Data* data_p) {

    switch(data_p->header.type) {
        case 1:
            printInfo(&data_p->header);
        case 2:
            printInfo(data_p->header);
        case 3:
            data_p->signal2->something[5];
    }   
}


void printInfo(Data* data_p) 
{
    data_p->header.primitive;
}
  • 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-26T00:27:04+00:00Added an answer on May 26, 2026 at 12:27 am

    In your example it looks like you want to rely on the fact that the type, protocol, clientId, serverId fields from Header, Signal1, Signal2 will be aligned the same way. But nothing in the C or C++ standard guarantees that AFAIK. So it would probably work with most C compilers but it is a hack, where such a hack is not necessary.

    It seems like it would be better to do things as follows :

    struct Header 
    {
        int type:
        int protocol;
        int clientId;
        int serverId;
    };
    
    struct Signal1
    {
        char something[80];
    };
    
    struct Signal2
    {
        char something[100];
    };
    
    struct Message
    {
        struct Header header;
        union
        {
            struct Signal1 signal1;
            struct Signal2 signal2;
        } payload;
    };
    

    As for your last question if I understand it correctly, the union is allocated enough memory for the largest of it’s members, and aligned at the strictest of it’s members’ boundaries, and at any point in time contains valid data for one of it’s members (but nothing indicating which is the correct one). And the pointer to union is the address of the start of that memory area.

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

Sidebar

Related Questions

I have question about throw. How will the throw work in the following code?
Possible Duplicate: A question about union in C Assuming the following code: #include <stdio.h>
I have a question about using streams in .NET to load files from disk.
I have a question about using os.execvp in Python. I have the following bit
I have question about my code which does the Hoare Partition Method. Here is
I have a question about union in C Language. The variables declared in a
I have question about python and sqlite3. I want to drop a table from
I have a question about union in c language for example: typedef struct {
I have question about try, catch and finally in Java. Consider the following scenario:
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag 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.