![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
What are unions?
What are unions?
I want to make a union like... Union u { char *cptr; short *sptr; int *iptr; } Then I would like to use it scroll through a list with those pointers, so I don't have to keep casting. Is this possible and is there a better way to do it? |
|
|
|
|
|
#2 | |
|
Newbie
Join Date: Nov 2005
Posts: 18
Rep Power: 0
![]() |
Why do you want to make a union when you don't know what its for?
A union is a data structure wich size is only as big as its biggest member, let me clarify: #include <iostream>
using namespace std;
int main()
{
union { char a; int b; } ;
a = 'a';
cout << "a is " << a << endl;
b = 0x99887762;
cout << "a is " << a << endl;
cout << "b is " << hex << b << endl;
a = 'A';
cout << "a is " << a << endl;
cout << "b is " << hex << b << endl;
cin.get();
return EXIT_SUCCESS;
}a is a a is b b is 99887762 a is A b is 99887741 In the above program I used an unnamed union, this way I dont have to access its members using a union object. An unnamed union is a union wich has not been defines as a type or a named object. blah. char a; and int b; overlaps in memory. They both start at the same address in memory. This way together, they only take up as much space as the biggest one, wich is b. If I write to a, b will change. However a will only change if I write to b's LO byte(first in memory, little endian). Quote:
|
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
A union is a type that has all its members occupying the same memory. So, in volatile's example &a == &b (if we ignore the types of a and b). The intent of a union is that it holds only one value at a time.
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
I've got what he wants -
In Windoze there is a variant datatype - which is represented in MS C by a big union. This union allows you to reference a BSTR then use it as an int or a float. Except: in vanilla C the void * does this for you. So you probably don't want a union. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: May 2006
Location: West Jordan, Utah, United States
Posts: 176
Rep Power: 3
![]() |
I am trying to help write a client application for a game server as I mentioned a while ago. Someone else sent me some of the code they were working on in the same project. Here is some of their code:
...
switch(*msgType) {
// server load
case SV_LOAD:
msgType+=6;
break;
// server tick
case SV_TICK:
msgType+=2;
break;
// setting the name of our own char (in 3 parts)
case SV_SETCHAR_NAME1:
memcpy(cplayer.name,msgType+1,15);
msgType+=16;
break;
case SV_SETCHAR_NAME2:
memcpy(cplayer.name+15,msgType+1,15);
msgType+=16;
break;
case SV_SETCHAR_NAME3:
memcpy(cplayer.name+30,msgType+1,10);
msgType+=16;
break;
// setting mode (speed)
case SV_SETCHAR_MODE:
cplayer.mode = *(msgType+1);
msgType+=2;
break;
// setting attributes (str, int, will, brav)
case SV_SETCHAR_ATTRIB:
cplayer.attrib[*(msgType+1)][0]=*(msgType+2);
cplayer.attrib[*(msgType+1)][1]=*(msgType+3);
cplayer.attrib[*(msgType+1)][2]=*(msgType+4);
cplayer.attrib[*(msgType+1)][3]=*(msgType+5);
cplayer.attrib[*(msgType+1)][4]=*(msgType+6);
cplayer.attrib[*(msgType+1)][5]=*(msgType+7);
msgType+=8;
break;
// setting skills
case SV_SETCHAR_SKILL:
cplayer.skill[*(msgType+1)][0]=*(msgType+2);
cplayer.skill[*(msgType+1)][1]=*(msgType+3);
cplayer.skill[*(msgType+1)][2]=*(msgType+4);
cplayer.skill[*(msgType+1)][3]=*(msgType+5);
cplayer.skill[*(msgType+1)][4]=*(msgType+6);
cplayer.skill[*(msgType+1)][5]=*(msgType+7);
msgType+=8;
break;
// setting hp
case SV_SETCHAR_HP:
cplayer.hp[0]=*(unsigned short*)(msgType+1);
cplayer.hp[1]=*(unsigned short*)(msgType+3);
cplayer.hp[2]=*(unsigned short*)(msgType+5);
cplayer.hp[3]=*(unsigned short*)(msgType+7);
cplayer.hp[4]=*(unsigned short*)(msgType+9);
cplayer.hp[5]=*(unsigned short*)(msgType+11);
msgType+=13;
break;
// setting endurance
case SV_SETCHAR_ENDUR:
cplayer.end[0]=*(unsigned short*)(msgType+1);
cplayer.end[1]=*(unsigned short*)(msgType+3);
cplayer.end[2]=*(unsigned short*)(msgType+5);
cplayer.end[3]=*(unsigned short*)(msgType+7);
cplayer.end[4]=*(unsigned short*)(msgType+9);
cplayer.end[5]=*(unsigned short*)(msgType+11);
msgType+=13;
break;
// setting mana
case SV_SETCHAR_MANA:
cplayer.mana[0]=*(unsigned short*)(msgType+1);
cplayer.mana[1]=*(unsigned short*)(msgType+3);
cplayer.mana[2]=*(unsigned short*)(msgType+5);
cplayer.mana[3]=*(unsigned short*)(msgType+7);
cplayer.mana[4]=*(unsigned short*)(msgType+9);
cplayer.mana[5]=*(unsigned short*)(msgType+11);
msgType+=13;
break;
// setting items
case SV_SETCHAR_ITEM:
cplayer.item[*(unsigned long*)(msgType+1)]=*(short int*)(msgType+5);
cplayer.item_p[*(unsigned long*)(msgType+1)]=*(short int*)(msgType+7);
msgType+=9;
break;
// setting equipped items
case SV_SETCHAR_WORN:
cplayer.worn[*(unsigned long*)(msgType+1)]=*(short int*)(msgType+5);
cplayer.worn_p[*(unsigned long*)(msgType+1)]=*(short int*)(msgType+7);
msgType+=9;
break;
...Thank you very much for your clear responses, you have made it clear what a union is and that it will be a good tool for my work. : ) |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jan 2006
Location: Some where
Posts: 74
Rep Power: 3
![]() |
Honesetly, what's the point of unions.... What would be a practical use for them?
|
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
They're a lot less useful now, but think back to when 1 Mb of memory was regarded as way too much. Space-saving measures were implemented any way they could be.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|