PDA

Click to See Complete Forum and Search --> : typedef


Sam Finch
May 4th, 2000, 03:02 AM
Hmm, I seem to be the guy asking all the questions on this forum. I apreciate all the answers.

I seem to remember somewhere that you could use typedef to make a new variable type that could contain one of two variables. so If I made a type OptionalSignedLong that could hold either a signed or unsigned long I could go

OptionalSignedLong MyNumber = 10;

MyNumber = (signed long)-10; /* MyNum Stores a
signed long*/


MyNumber = (unsigned long) 10000; /* MyNum Stores an
unsigned long*/


Is this possible or am i just imaganing things.

May 4th, 2000, 08:23 AM
You can do it exactly the way you have it, if you type something like

typedef UnLong unsigned long;

and the such.

Sam Finch
May 4th, 2000, 09:31 AM
Sorry, I didn't make Myself Clear, I want the same data type to hold both types, Kinda Like A Variant in VB only specific to 2 types. I'm SURE I saw this somewhere.

noone
May 4th, 2000, 09:46 AM
Do you mean an union?
and I quote:
"Unions are similar to structures. A union is declared and used in the same ways that a structure is. A union differs from a structure in that only one of its members can be used at a time. The reason for this is simple. All the members of a union occupy the same area of memory. They are laid on top of each other.

union shared {
char c;
int i;
};

Sam Finch
May 4th, 2000, 07:10 PM
D'OH

Thanks a lot, It's been staring me in the face but I thought unions were for transmitting data between namespaces or something.

PT Exorcist
Oct 6th, 2003, 07:51 AM
i dont quite get the why of unions even with ur explication..let me see:

union shared {
char c;
int i;
};

now if i put

c = 'A';

if i understand well the theory if the vars are on the same memory they share the memory right?
then the i variable will change to 65(A = 65)?

CornedBee
Oct 6th, 2003, 01:21 PM
Possibly. But since char is one byte long and int four (usually), the char might occupy either the first or the last byte of the integer, depending on the compiler and the CPU.
So since 'A' is 41 in hex, the int might be
00 00 00 41 = 65
or
41 00 00 00 = 1,090,519,040

PT Exorcist
Oct 6th, 2003, 01:32 PM
much tks by the answer! actually it was a mistake of my part to think they would have the same values as i forgot that char is like a byte and not like an int

could u give me an real world example of using an union? are they very used in c++?

CornedBee
Oct 6th, 2003, 01:46 PM
Hardly at all. The VB Variant datatype is actually a union in the C/C++ backend.

There are some tricky float/int conversion optimizations for high-performace/low-accuracy apps (like games) that involve a union like this:
typedef union {
float f;
int i;
} FICONVERT;
You can read about this in the Game Programming Gems series.

Then there's the LARGE_INTEGER and ULARGE_INTEGER unions in the Win32 API, which are unions of a 64-bit integer and a struct of two 32-bit integers, to support 64-bit numbers on machines which don't support them. Used to retrieve free disk space for example.

There were the register unions back in the DOS days. They were used to store the CPU register state, and as CPU registers partially overlap a union is the perfect choice.

struct general_purpose
{
union {
long eax;
short ax;
struct {
char al;
char ah;
};
};
union {
long ebx;
short bx;
struct {
char bl;
char bh;
};
};
union {
long ecx;
short cx;
struct {
char cl;
char ch;
};
};
union {
long edx;
short dx;
struct {
char dl;
char dh;
};
};
};

PT Exorcist
Oct 7th, 2003, 10:39 AM
so as noob i shouldnt really care about it do i?

CornedBee
Oct 7th, 2003, 11:28 AM
Exactly