memcpy problem

J

jack

hello all,
how to do a memcpy to a structure from an integer (uint32)

struct temp_t *y;
uint32 x = 0x12345677;

I have to assign this address to a structure through memcpy
I tried in the following way

memcpy(y , (temp_t*)x , sizeof(x));

but it doesn't work ? How to solve this ?

what if If i do in the following way: y = (temp_t*)x;

Thank you for any help
 
M

Mark Bluemel

hello all,
how to do a memcpy to a structure from an integer (uint32)

struct temp_t *y;
uint32 x = 0x12345677;

I have to assign this address to a structure through memcpy
I tried in the following way

memcpy(y , (temp_t*)x , sizeof(x));

but it doesn't work ? How to solve this ?

what if If i do in the following way: y = (temp_t*)x;

Thank you for any help

I think you need to be more explicit about what you are trying to
achieve with this rather perverse-looking technique...
 
J

Jens Thoms Toerring

jack said:
how to do a memcpy to a structure from an integer (uint32)
struct temp_t *y;

Now 'y' is an uninitialized pointer. It points to some random
place in memory, not to a place you could (or at least try to)
copy anything to.
uint32 x = 0x12345677;
I have to assign this address to a structure

This isn't an address, this is a integer.
through memcpy I tried in the following way
memcpy(y , (temp_t*)x , sizeof(x));
but it doesn't work ? How to solve this ?

Now you copy a number of bytes (as many as are needed to
store a number of type uint32_t) from an address that you
obtained by converting the number 'x' to some random
place in memory. That doesn't sound like a good plan.
what if If i do in the following way: y = (temp_t*)x;

This would do something completely different - it would
make the pointer 'y' point to the address you get when
you convert the number 'x' to a pointer. No copying in-
volved here.

Unfortunately, it's completely unclear what all this
is supposed to do. What exactly means "I have to assign
this address to a structure"? Do you want to store that
address in some member of a structure? Or do you want
to make 'y' a pointer to some address because you know
that at that address there are data layed out in exactly
the same way as they are in a structure of type 'temp_t'?
Or is it something else?
Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top