Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   memcpy problem (http://www.velocityreviews.com/forums/t744172-memcpy-problem.html)

jack 02-25-2011 09:20 AM

memcpy problem
 
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

Mark Bluemel 02-25-2011 09:28 AM

Re: memcpy problem
 
On Feb 25, 9:20*am, jack <crazycompetit...@googlemail.com> 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...

Jens Thoms Toerring 02-25-2011 12:12 PM

Re: memcpy problem
 
jack <crazycompetition@googlemail.com> 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 ___ jt@toerring.de
\__________________________ http://toerring.de


All times are GMT. The time now is 07:29 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.