memcpy problem

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

  1. jack

    jack Guest

    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
    #1
    1. Advertising

  2. jack

    Mark Bluemel Guest

    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
    #2
    1. Advertising

  3. 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
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ninan Thomas
    Replies:
    3
    Views:
    403
    Kevin Goodsell
    Aug 22, 2003
  2. Bren
    Replies:
    8
    Views:
    2,008
    Stephen Howe
    Sep 4, 2003
  3. Paul Schouten
    Replies:
    2
    Views:
    2,415
    Vladimir Khvostov
    Jan 3, 2005
  4. ronny

    memcpy problem

    ronny, Feb 13, 2005, in forum: C++
    Replies:
    2
    Views:
    4,668
    ronny
    Feb 13, 2005
  5. Ninan Thomas
    Replies:
    3
    Views:
    1,880
    Kevin Goodsell
    Aug 22, 2003
Loading...

Share This Page