# memcpy problem

Discussion in 'C Programming' started by jack, Feb 25, 2011.

1. ### jackGuest

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

jack, Feb 25, 2011

2. ### Mark BluemelGuest

On Feb 25, 9:20 am, jack <> wrote:
> 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...

Mark Bluemel, Feb 25, 2011

3. ### Jens Thoms ToerringGuest

jack <> wrote:
> 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
--
\ Jens Thoms Toerring ___
\__________________________ http://toerring.de

Jens Thoms Toerring, Feb 25, 2011