Soewandi Wirjawan wrote:

>
> if i have this
>
> char buffer[5];
> int temp[4];
>
> buffer[0] = 'A';
> temp[0] = 3;
> temp[1] = 1;
> temp[2] = 2;
> temp[3] = 2;
>
> and i do this: memcpy(buffer+1, &temp, sizeof(temp));
> will the buffer contain A3122? because that's the
> result that i wanted, and somehow i only got the A
> part ...

No, you can't do this with memcpy, because an integer takes more memory 
than a character, on most systems, an integer takes 4 bytes, a character 
only 1. So the array temp would have 16 bytes, buffer only 5. So you 
would even copy more memory to buffer than it contains, very dangerous. 
The best way to do what you want to do is with a for loop:

    for (int i=0; i<4; ++i)
        buffer[i+1] = temp[i];

And be careful that the integers in temp are not 128 or more. Or less 
than -127.

-- 
  Mark Van Peteghem
  http://www.q-mentum.com -- easier and more powerful unit testing






------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~-> 

To unsubscribe : [EMAIL PROTECTED]

 
Yahoo! Groups Links

<*> To reply to this message, go to:
    http://groups.yahoo.com/group/Programmers-Town/post?act=reply&messageNum=3618
    Please do not reply to this message via email. More information here:
    http://help.yahoo.com/help/us/groups/messages/messages-23.html

<*> To visit your group on the web, go to:  
    http://groups.yahoo.com/group/Programmers-Town/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to