這裏是我的代碼:的Dynamic C - 字符指針,strcpy的,strcat的
nodebug void sendLogPacketS(char *func, char *msg)
{
char * log;
memset(log, 0, strlen(func) + strlen(msg) + 1);
strcpy(log, func);
strcat(log, ": ");
strcat(log, msg);
sendUDPLogPacket(log, strlen(log));
}
它應該採取兩個字符串,拼接在一起,然後通過新的字符串,其長度不同的功能。我使用Dynamic C 9.62,它不支持malloc
函數,所以我使用memset代替。
問題是我在printf的值log
傳遞到sendUDPLogPacket
之前,它包含垃圾DynamiCUniversal Rabbit BIOS Version 9.50\?^>j
。任何人有任何想法,爲什麼這不起作用?
'memset()'填充已分配的內存。它不*分配內存。 'log'指向一個隨機的位置(幸運的是)在你的情況下似乎不是可寫的,所以你打印錯誤的數據而不是崩潰。 –