pointer help :D

Started by
3 comments, last by Mantear 14 years, 2 months ago
DWORD X = 5; // X = 5 ; DWORD * Y = &X ; // Y = address of X DWORD copy = *Y ; // copy is now = 5 , dereferenced Y byte bytecopy = *(byte*)Y ; // point to the address of Y but only copy 8 bits instead of 4 bytes and get the value those 8 bits produce and store it in bytecopy? am i understanding the last statement correctly? byte bytecopy = *(byte*)Y ; // point to the address of Y but only copy 8 bits instead of 4 bytes and get the value those 8 bits produce and store it in bytecopy? c
Advertisement
Hi,

Yes, (byte*)Y points to one of the 4 bytes making up the DWORD. So the result is whatever that byte contains.
Ron AF Greve
how would this ever be usefull in a real programming example...lol j/w
Hi,

Well for instance in the following case. I have a serialization system in C++. At the lowest level when writing something to disk (whether string type, char * long etc. etc) it will write it as a sequence of bytes. So at the lowest level I do actually write every individual byte (okay I use it as a character buffer but the principal stays the same ).

stream.write( (char*)Buffer, sizeof( Type ) );

Now type can be any integer type (string needs some specialization, i.e. a separate function since it needs to store length and the character buffer in string).

So any data will eventually end up as a stream of characters.

However in my code I rarely dereference a single byte from a larger datatype. The above is one example, other examples might be working with colors where you have an ARGB layout and you need to convert to another layout like RGBA.

[Edited by - Ron AF Greve on February 7, 2010 9:30:45 AM]
Ron AF Greve
You would need something like this if you need to serialize some object data into a byte stream for transfering across some external interface.

This topic is closed to new replies.

Advertisement