我的strncat版本是将一个太多的字符复制到目的地,而我不知道为什么。
#include <stdio.h>
#define MAX_CHARS 20
void nconcatenate(char *start, char *end, int n)
{
if(sizeof start + n > MAX_CHARS)
return;
while(*start++);
start--; /* now points to the final char of start, the \0 */
int i;
for(i = 0; (*start++ = *end++) && i < n; i++);
*start = '\0';
}
int main()
{
char start[MAX_CHARS] = "str";
char *end = "ingy!";
nconcatenate(start, end, 3);
printf("start = %s\n", start);
return 0;
}使用3作为“n”输出
stringy这是一个太多的字符。
发布于 2014-07-22 18:31:39
也许是因为在这种情况下
(*start++ = *end++) && i < n首先检查(*start++ = *end++),然后检查i< n。
我还没试过,但是看看吧。
https://stackoverflow.com/questions/24894938
复制相似问题