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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:59:15+00:00 2026-05-20T07:59:15+00:00

The following code compiles and runs fine with gcc. But i wondered if such

  • 0

The following code compiles and runs fine with gcc. But i wondered if such an union is define by the standard and if it works on all c compilers the same way. I know it will not work if, parameters of that functions are not pointers and not compatible with each other, but as long as all paramters are pointers and the number of parameters are the same there shouldn’t be a problem, or?

typedef struct node {
    unsigned long int key;
} node_t;

typedef struct node1 {
    unsigned long int key;
    char *str;
} node1_t;

typedef struct node2 {
    unsigned long int key;
    void *data;
} node2_t;

typedef struct node3 {
    unsigned long int key;
    int numbers[256];
} node3_t;


int compare(node_t *a, node_t *b) {

    printf("%ld ? %ld\n", c->key, d->key);
    return c->key == d->key;
}

struct comp {
    union {
        int (*c0) (node_t  *a, node_t  *b);
        int (*c1) (node1_t *a, node1_t *b);
        int (*c2) (node1_t *a, node2_t *b);
        int (*c3) (node1_t *a, node3_t *b);
        int (*c4) (node2_t *a, node1_t *b);
        int (*c5) (node2_t *a, node2_t *b);
        int (*c6) (node2_t *a, node3_t *b);
        int (*c7) (node3_t *a, node1_t *b);
        int (*c8) (node3_t *a, node2_t *b);
        int (*c9) (node3_t *a, node3_t *b);
    } are;
};


int main(int argc, char *argv[])
{
    node1_t a[] = {
        { 23477843258923UL, "Hello World" },
        { 10254892378892UL, "Hello Back" }
    };
    node2_t b[] = {
        { 83296783479803UL, NULL },
        { 52348237489832UL, (void *) &a[1] }
    };
    node3_t c[] = {
        { 91308823949203UL, { 3, 4, 5 } },
        { 17587832478823UL, {43, 43, 43, 86 } }
    };

    struct comp comp;
    comp.are.c0 = compare;

    comp.are.c1(&a[0], &a[1]);
    comp.are.c2(&a[1], &b[0]);
    comp.are.c3(&a[0], &c[1]);
    comp.are.c8(&c[1], &b[1]);
}
  • 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-20T07:59:16+00:00Added an answer on May 20, 2026 at 7:59 am

    The union is valid, but what you are doing with it is not. You cannot put something into the union with one type and then use it with another like the end of main is doing unless those types are convertible (thanks to @Christoph for that comment).

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

Sidebar

Related Questions

The following code compiles in Visual C++ and gcc, but fails with Code Warrior
I have the following code which compiles and runs fine under Visual Studio 2008
I have written the following simple C code, which compiles and runs fine. However
The following code is crashing my application on startup. It compiles just fine but
The following code compiles and runs fine. void myInvokedMethod(string s) { Console.WriteLine(s); } void
the following code compiles with Visual Studio 2008 but not with g++ on Mac
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
I have the following code, which compiles but doesn't bring back any data. Here

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.