При динамическом выделении памяти в C для строк учитывается ли \0 конец строки char?

При динамическом выделении памяти в C для строк учитывается ли конец \0 строки char?

char *copyInto, *copyFrom="test";

// Should 
copyInto = (char*)malloc(strlen(copyFrom));
// suffice?

// or should this be the following?
copyInto = (char*)malloc(strlen(copyFrom)+1);

// assuming you want to copy the string from copyFrom into copyInto
strcpy(copyInto,copyFrom);

// Does anyone recommend just \0-ing the whole copyInto as in
copyInto = (char*)calloc(strlen(copyFrom)+1);
// and if so, should it still be (strlen(copyFrom)+1) size?
0
задан 8 March 2012 в 00:46
поделиться