Find the answer to your Linux question:
Results 1 to 4 of 4
Why transparent union gives error #include<stdio.h> #include<conio.h> typedef union __attribute__((transparent_union)) { float b; int a; }_s; void f(_s s); int main(int argc,char *argv[]) { float a=12.210; f(a); getch(); return 0; ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2009
    Posts
    7

    Transparent union


    Why transparent union gives error
    #include<stdio.h>
    #include<conio.h>

    typedef union __attribute__((transparent_union))
    { float b;
    int a;
    }_s;

    void f(_s s);

    int main(int argc,char *argv[])
    { float a=12.210;
    f(a);
    getch();
    return 0;
    }
    void f(_s s)
    { printf("%d\n%f\n",s.a,s.b);
    }

    It gives following errors:
    Union cant be made transparent
    Incomplete type for argument 1 of f

    Thanks.

  2. #2
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by sukhdeep View Post
    Why transparent union gives error
    #include<stdio.h>
    #include<conio.h>

    typedef union __attribute__((transparent_union))
    { float b;
    int a;
    }_s;

    void f(_s s);

    int main(int argc,char *argv[])
    { float a=12.210;
    f(a);
    getch();
    return 0;
    }
    void f(_s s)
    { printf("%d\n%f\n",s.a,s.b);
    }

    It gives following errors:
    Union cant be made transparent
    Incomplete type for argument 1 of f

    Thanks.
    When you post, please:

    1. Use code tags when posting code.
    2. Copy and paste error messages.


    The error I get for the second line is incompatible type for... which is a much different error than having an incomplete type. Also, line numbers help and cutting and pasting gives us these.

    Anyway, you cannot make this union transparrent because, as per the GCC docs, all members of a transparrent union must have the same "machine representation".
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

  3. #3
    Just Joined!
    Join Date
    Apr 2009
    Posts
    7
    Thanks JohnGraham,
    It means transparent union can be used for char and long variable. But i seen that order of placement of variable gives error. How can i know for what type of variable and in which order transparent union will work.

  4. #4
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by sukhdeep View Post
    But i seen that order of placement of variable gives error. How can i know for what type of variable and in which order transparent union will work.
    I don't know but like I said, according to the GCC docs all members of a transparent must have the same machine representation.

    I get that you may be experimenting, but to be honest my advice would be to avoid using this feature and just use unions "as the standard intended", as it were.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •