You seem to be confusing two entirely separate concepts and/or asking two completely unrelated questions. I'll start with the pointer question:
(how can I point foo2 to = &foo)
foo2 can't point to foo because foo2 isn't a pointer.
If you want something to point to foo, then you can use a pointer:
1 2 3
unsignedshort foo = 1337;
unsignedshort* fooptr = &foo; // <- now fooptr points to foo
How do I have foo2, contain the binary equivilant of foo?
This has nothing really to do with pointers, and more to do with copying binary data.
1337 in hex is 0x0539. So the 2 bytes that you'd want assigned here are 0x05 and 0x39. (Note again that these are not pointers and this has nothing to do with pointers).
You have two options. The first is to use a dumb memory copy function to copy the binary data. IE, memcpy:
1 2 3 4 5 6 7 8 9 10 11 12
unsignedshort foo = 1337;
char foo2[2];
// technique 1: use memcpy
memcpy( foo2, &foo, 2 );
// at this point:
// foo2[0] == 0x39
// and
// foo2[1] == 0x05
// (assuming you're on a little endian system. On a big endian system those will
// be flipped)
The other option is to do it manually:
1 2 3 4 5 6 7 8 9 10 11 12
unsignedshort foo = 1337;
char foo2[2];
// technique 2: manual copy
foo2[0] = char( foo & 0xFF );
foo2[1] = char( (foo >> 8) & 0xFF );
// at this point:
// foo2[0] == 0x39
// and
// foo2[1] == 0x05
// REGARDLESS of system endianness.
I'm dabbling in some winsock tutorials, and everything received in a char* buffer, regardless of datatype, so I was trying to figure out how to match the data to appropriate variables for handling.
Not used to using mem functions, looks like I gotta go back to some basics again before proceeding forward.
Also there is other thing which you need to know about.
since unsigned short is 2 bytes
is technically false. unsignedshort size is equal or larger than 16 bits. It can be 1 (for systems with 16 bit chars) or 4 (for systems wwith 8 bit char but with 32 bit shorts)
Though sizeof(unsignedshort) is 2 on majority PC compilers, you should not rely on this behavior.
You should either get size of type in compile time, or make sure it is 2 before doing anything: