By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,628 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Cast to a struct?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a

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.