Reply embedded...

> -----Original Message-----
> From: Soewandi Wirjawan [mailto:[EMAIL PROTECTED]
> Sent: Thursday, October 21, 2004 4:28 PM
> To: [EMAIL PROTECTED]
> Subject: Re: (PT) help memcpy
> 
> 
> 
> 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, 

If you want 'buffer' to contain:
    'A', '3', '1', '2', '2'

Then, the answer is no.  

Also, your buffer[] is not big enough to store a 5 characters string.
Remember in C, a string needs a nul character at the end to signify its
termination, and you don't have room for it.


> and somehow i only got the A
> part ...

Since temp[4] is of type int, and on systems where sizeof(int) !=
sizeof(char), you'll be copying more than buffer could hold.  

Even for systems where sizeof(int) do equal sizeof(char), you're not copying
the character representation of the numeric value.  Buffer contains the
following instead:
    'A', '\x03', '\x01', '\0x02', '\0x02'

The difference between '0x02' and '2' is:

'\x02' is a value.  

'2' is a printable character.  Its value depends on the underlying encoding
system.  On ASCII system, its value is 0x32.  On EBCDIC, its value is 0xF2.

memcpy() duplicate bytes.  It will not do conversion.

>From your question, it seems like you want to store the character
representation of some decimal value in buffer.  One portable way is to use
sprintf.

    #define MAX_SIZE 6
    char Buffer[MAX_SIZE];

    ...
    sprintf(Buffer, "A%d%d%d%d", temp[0], temp[1], temp[2], temp[3]);


There is one condition:  You have to ensure that all elements in temp are in
the ranged of [0, 9]:

    for(n = 0; n < 4; ++n)
    {
        assert(temp[n] >= 0 && temp[n] < 10);
    }

Show a bigger picture of how you get the values for 'temp' (codes and
explanation).  I got a feeling that your misunderstanding might also mass up
somewhere. 

HTH
Shyan








------------------------ 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=3622
    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