Skip to main content

Thread: This union code isn't working


hi -
i've done bit of code (to play around making "tuple" in c).
code:
     #include <string.h>     #include <stdio.h>   #include <stdlib.h>        /* ints */   typedef struct  {        int type ;        int val;   }  myint;       /* strings */   typedef struct  {        int type ;        char *val;   }  mystr;       /* chars */   typedef struct  {        int type ;        char val;   }  mychar;       /* floats */   typedef struct {        int type ;        float val;   }  myfloat;       /* combine them union. */   typedef union {    	     myint    *i;            mystr    *s;            mychar   *c;            myfloat  *f;   }  mytype ;       void myfunc(mytype *mt)   {          if (mt->i->type == 1)  puts("m integer \n");               else if (mt->s->type == 2)  puts("m array \n");     else if (mt->c->type == 3)  puts("m char \n");     else if (mt->f->type == 4)  puts("m float \n");        }       int main(void)   {   	  myint foo;  foo.type = 1;   foo.val = 123;     mystr bar;   bar.type = 2;   bar.val = "testing..." ;     mychar baz;   baz.type = 3;   baz.val = '@' ;    myfloat abc;   abc.type = 4;   abc.val = 42.42 ;     /* create unions */   mytype *test1;   test1.i.type = foo.type;  test1.i.val  = foo.val;        mytype *test2;    test2.s.type = bar.type;  test2.s.val = bar.val;     mytype *test3;    test3.c.type = baz.type;  test3.c.val = baz.val;     mytype *test4;    test4.f.type = abc.type;  test4.f.val = abc.val;    			  return 0; 	  	  }
however, when compiled, these messages appear -
** start of messages **
gcc -wall -o "pseudo_typeof2" "pseudo_typeof2.c" (in directory: /home/andy/pd_pseudo_typeof)
compilation failed.
pseudo_typeof2.c: in function ‘main’:
pseudo_typeof2.c:84:6: error: request member ‘i’ in not structure or union
pseudo_typeof2.c:85:6: error: request member ‘i’ in not structure or union
pseudo_typeof2.c:88:6: error: request member ‘s’ in not structure or union
pseudo_typeof2.c:89:6: error: request member ‘s’ in not structure or union
pseudo_typeof2.c:92:6: error: request member ‘c’ in not structure or union
pseudo_typeof2.c:93:6: error: request member ‘c’ in not structure or union
pseudo_typeof2.c:96:6: error: request member ‘f’ in not structure or union
pseudo_typeof2.c:97:6: error: request member ‘f’ in not structure or union
pseudo_typeof2.c:95:9: warning: variable ‘test4’ set not used [-wunused-but-set-variable]
pseudo_typeof2.c:91:9: warning: variable ‘test3’ set not used [-wunused-but-set-variable]
pseudo_typeof2.c:87:9: warning: variable ‘test2’ set not used [-wunused-but-set-variable]
pseudo_typeof2.c:83:9: warning: variable ‘test1’ set not used [-wunused-but-set-variable]
** end of messages **

have no problem structs, unions seem problem.
- going on here, , how can fixed.....?
many in advance -
- t

read difference between . , ->.

edit: , have big problem let figure out yourself...


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk This union code isn't working


Ubuntu

Comments

Popular posts from this blog

Thread: Can not create raid array: mdadm: no raid-devices specified.

Thread: HOW TO: Package and theme GTK+ / Gtkmm apps in Linux for Windows

Thread: Twinview issues