Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Cast to a struct?

Reply
Thread Tools

Cast to a struct?

 
 
Jack
Guest
Posts: n/a
 
      07-06-2006
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;
}
 
Reply With Quote
 
 
 
 
Jack
Guest
Posts: n/a
 
      07-06-2006
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.
 
Reply With Quote
 
 
 
 
chy1013m1@gmail.com
Guest
Posts: n/a
 
      07-06-2006
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;
}

 
Reply With Quote
 
dan2online
Guest
Posts: n/a
 
      07-06-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is the result of valid dynamic cast always equal to the result ofcorrespondent static cast? Pavel C++ 7 09-18-2010 11:35 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM
how to cast system.intptr to struct BestNews ASP .Net 1 09-03-2003 09:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57