When I have a string of unknown size and I create an char array to hold this string.
I do something like this:
#define LINE_END "\r\n"
int line_end_size = strlen(LINE_END);
char line[]="Hello! world.";
int line_size=strlen(line);
int test_line_size=line_size+line_end_size;
char *test_line = malloc(test_line_size);
strcpy(test_line,line);
strcat(test_line,LINE_END);
printf("test_line = ");
int index;
for (index = 0; index<test_line_size+1; index++) /* plus 1 to see the null */
printf("%02X ", (unsigned char)test_line[index]);
printf("\n");
Should I add 1 to the size of test_line_size
to hold the NULL character?
This must be compiled for linux32, win32, arm32.
memcpy()
. The "str*" functions are a waste of time, looking for the zero terminator when you already know the length.