memcpy Problem

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
ADePSP
Posts: 58
Joined: Thu Jul 13, 2006 9:07 pm

memcpy Problem

Post by ADePSP »

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: Select all

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...?
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

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)
ADePSP
Posts: 58
Joined: Thu Jul 13, 2006 9:07 pm

Post by ADePSP »

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...
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

you should still remove the memcpy call bacause it's an unnedded lost of resources (it's still a function call)
Post Reply