Re: confused about copying strings



Ian Collins wrote:
On 07/18/10 11:11 AM, Richard Heathfield wrote:
Angus wrote:
The sample code below simulates receiving a protocol message with a
tlv structure - type/length/data_value.

I am confused about copying the actual data to a temp string.

Since you know in advance that you have sufficient storage to store the
copy, use strcpy.

int dtype = *mybytes++;
int dsize = *mybytes++;
size_t size = dsize;
char *data = malloc(size);
if(data != NULL)
{
if(strlen(mybytes) < size)

That would would work in the special case here of a string literal, but probably wouldn't be much use with data over the wire, unless it was null terminated.

I checked the OP's code, and his end test is for null termination. I assumed, therefore, that he has null-terminated data.

<meta>
It's odd that Thunderbird has munted your formatting, but no the OPs
</meta>

Thunderbird is indeed odd. But KNode is all the way upstairs, and it's /such/ a long way. And someone's nicked my chair from that desk.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
.