hi all,
can we declare a variable like int char a[];
is it possible. ...
any help greatly encouraged
can we have variable with 2 datatypes
Re:can we have variable with 2 datatypes
With basic types, no ...
As in: What should this be then?
As in: What should this be then?
Re:can we have variable with 2 datatypes
neopro wrote: hi all,
can we declare a variable like int char a[];
is it possible. ...
any help greatly encouraged
Code: Select all
union {
char a[4];
int b;
} variable;
variable.b = 0x12345678;
printf("%c", variable.a[2]); // prints 56 on big endian systems, 34 on little endian systems, Bus Error on some systems :D
Re:can we have variable with 2 datatypes
There is no way to define two non-complex types in one memory location. You can cheat your way out of it using complex pointers, but they're not easy either.Legend wrote: Yet this is a complex type!
The union type has been created to make two things appear in one place, there's no way around it. BTW,
typedef union {
...
} new_type;
pretty much same as a normal type.
Re:can we have variable with 2 datatypes
Perhaps a better question is, What are you trying to accomplish, and why do you think that this would be a solution to your problem? What is your goal in doing this?