| View previous topic :: View next topic |
| Author |
Message |
ADePSP
Joined: 13 Jul 2006 Posts: 58
|
Posted: Sat Sep 30, 2006 1:14 am Post subject: memcpy Problem |
|
|
I'm having some problems with the memcpy function in C... I have a pointer to a chunk of data and I simply want to copy 2 bytes from it into a short...
See the code snippit,
| Code: | char* ptrData;
unsigned short myShort;
ptrData = getData();
memcpy((char*)&myShort, ptrData, 2);
printf("ptrData = %d\n",(unsigned short)ptrData);
printf("myShort = %d\n",myShort); |
You would expect the output values to be the same but they are not... What I get is,
ptrData = 23568
myShort = 36096
Can anyone see what i'm doing wrong and why myShort has the wrong number in it...? |
|
| Back to top |
|
 |
weltall
Joined: 20 Feb 2004 Posts: 310
|
Posted: Sat Sep 30, 2006 1:33 am Post subject: |
|
|
first of all what is the declaration of getdata and why ptrdata is a char when you are using it to store a shorth?
second you can't do a simple myshort = *(unsigned short*)ptrdata (didn't test it :P) |
|
| Back to top |
|
 |
ADePSP
Joined: 13 Jul 2006 Posts: 58
|
Posted: Sat Sep 30, 2006 3:32 am Post subject: |
|
|
| weltall wrote: | first of all what is the declaration of getdata and why ptrdata is a char when you are using it to store a shorth?
second you can't do a simple myshort = *(unsigned short*)ptrdata (didn't test it :P) |
It's ok, I worked it out... I was displaying the pointer of prtData rather than the data it contained... The memcpy did work and was correct... It was other mistakes that led me to believe otherwise...
Thanks anyway... |
|
| Back to top |
|
 |
weltall
Joined: 20 Feb 2004 Posts: 310
|
Posted: Sat Sep 30, 2006 3:56 am Post subject: |
|
|
| you should still remove the memcpy call bacause it's an unnedded lost of resources (it's still a function call) |
|
| Back to top |
|
 |
|