Defining a Union at a perticular address in C

Status
Not open for further replies.

nrmlguru

Newbie level 4
Joined
Jan 25, 2009
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,310
Hello,

Please help me to define a Union at particular address.

say at address 0x1234
 

you declare the union, define a pointer to the type and initialise it with the address
Code:
typedef	union
{
	unsigned long int value;
	char xch[2];
} TempData;

TempData *ptr= (TempData* )0x1234;
what happens if you use the pointer to access the object pointed to depends on the system you are using and if the pointer holds a valid address, e.g.
Code:
ptr->value=1;
if the pointer does not hold a valid address you could get an exception, address error, segmentation error, etc or it may appear to work corrupting memory contents and possibly causing a crash later.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…