Cast to a struct?

Discussion in 'C++' started by Jack, Jul 6, 2006.

  1. Jack

    Jack Guest

    Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a
    4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value
    contain information independent of each other. Is there a direct way
    to perform this cast? If not, is there way in C++ to do it?

    For example, I would like my function test() to work the way it is
    coded below. It produces a compiler error though:

    #include <pshpack2.h> // 16-bit padding

    // My 4-byte struct
    typedef struct _mystruct_t
    {
    unsigned short cx;
    unsigned short cy;
    } mystruct_t;

    #include <poppack.h>

    mystruct_t test()
    {
    unsigned int n = 0xffff0001;
    return (mystruct_t)n;
    }

    int main()
    {
    mystruct_t s = test();
    return 0;
    }
    Jack, Jul 6, 2006
    #1
    1. Advertising

  2. Jack

    Jack Guest

    On Wed, 05 Jul 2006 20:58:29 -0500, Jack <> wrote:

    >Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a
    >4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value
    >contain information independent of each other. Is there a direct way
    >to perform this cast? If not, is there way in C++ to do it?
    >
    >For example, I would like my function test() to work the way it is
    >coded below. It produces a compiler error though:
    >
    >#include <pshpack2.h> // 16-bit padding
    >
    >// My 4-byte struct
    >typedef struct _mystruct_t
    >{
    > unsigned short cx;
    > unsigned short cy;
    >} mystruct_t;
    >
    >#include <poppack.h>
    >
    >mystruct_t test()
    >{
    > unsigned int n = 0xffff0001;
    > return (mystruct_t)n;
    >}
    >
    >int main()
    >{
    > mystruct_t s = test();
    > return 0;
    >}


    Oops, posted to wrong group. Meant for comp.lang.c.
    Jack, Jul 6, 2006
    #2
    1. Advertising

  3. Jack

    Guest

    the following compiles for me, in C you will just be doing malloc()
    instead of new.

    #include <iostream>
    using namespace std;

    struct a{
    unsigned short cx;
    unsigned short cy;
    };

    a *test() {
    int *xx = new int[1];
    *xx = 0xffff0001;
    return (a*)xx;
    }

    int main() {
    a* bb = test();
    cout << bb->cy;
    delete [] bb;
    }
    , Jul 6, 2006
    #3
  4. Jack

    dan2online Guest

    Jack wrote:
    > Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a
    > 4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value
    > contain information independent of each other. Is there a direct way
    > to perform this cast? If not, is there way in C++ to do it?
    >
    > For example, I would like my function test() to work the way it is
    > coded below. It produces a compiler error though:
    >
    > #include <pshpack2.h> // 16-bit padding
    >
    > // My 4-byte struct
    > typedef struct _mystruct_t
    > {
    > unsigned short cx;
    > unsigned short cy;
    > } mystruct_t;
    >
    > #include <poppack.h>
    >
    > mystruct_t test()
    > {
    > unsigned int n = 0xffff0001;
    > return (mystruct_t)n;
    > }
    >


    try it:

    mystruct_t test()
    {
    union
    {
    unsigned int n ;
    mystruct_t mystruct;
    } un;
    un.n = = 0xffff0001;
    return un.mystruct;
    }
    dan2online, Jul 6, 2006
    #4
    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. BestNews

    how to cast system.intptr to struct

    BestNews, Sep 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    4,721
    BestNews
    Sep 3, 2003
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,055
    Dan Pop
    Feb 10, 2004
  3. EvilRix
    Replies:
    8
    Views:
    618
    Martin Dickopp
    Feb 14, 2004
  4. Chris Fogelklou
    Replies:
    36
    Views:
    1,353
    Chris Fogelklou
    Apr 20, 2004
  5. Pavel
    Replies:
    7
    Views:
    510
    Pavel
    Sep 19, 2010
Loading...

Share This Page