468,468 Members | 2,686 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,468 developers. It's quick & easy.

Cast to a struct?

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;
}
Jul 6 '06 #1
3 14162
On Wed, 05 Jul 2006 20:58:29 -0500, Jack <ja**@ss.comwrote:
>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.
Jul 6 '06 #2
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 '06 #3

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;
}

Jul 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

31 posts views Thread by Jamie Burns | last post: by
4 posts views Thread by William Payne | last post: by
7 posts views Thread by James Mcguire | last post: by
11 posts views Thread by Alberto Giménez | last post: by
3 posts views Thread by Pablo Gutierrez | last post: by
28 posts views Thread by Tamir Khason | last post: by
6 posts views Thread by Jack | last post: by
2 posts views Thread by lovecreatesbea... | last post: by
17 posts views Thread by arnuld | last post: by
7 posts views Thread by * Tong * | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.