C语言 为什么我首先必须在 strcat() 之前使用 strcpy()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18838933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do i first have to strcpy() before strcat()?
提问by Or Cyngiser
Why does this code produce runtime issues:
为什么这段代码会产生运行时问题:
char stuff[100];
strcat(stuff,"hi ");
strcat(stuff,"there");
but this doesn't?
但这不是吗?
char stuff[100];
strcpy(stuff,"hi ");
strcat(stuff,"there");
回答by Tim
strcatwill look for the null-terminator, interpret that as the end of the string, and append the new text there, overwriting the null-terminator in the process, and writing a new null-terminator at the end of the concatenation.
strcat将查找空终止符,将其解释为字符串的结尾,并将新文本附加到那里,在此过程中覆盖空终止符,并在连接的末尾写入一个新的空终止符。
char stuff[100]; // 'stuff' is uninitialized
Where is the null terminator? stuffis uninitialized, so it might start with NUL, or it might not have NUL anywhere within it.
空终止符在哪里? stuff未初始化,因此它可能以 NUL 开头,也可能在其中的任何地方都没有 NUL。
In C++, you can do this:
在 C++ 中,你可以这样做:
char stuff[100] = {}; // 'stuff' is initialized to all zeroes
Now you can do strcat, because the first character of 'stuff' is the null-terminator, so it will append to the right place.
现在你可以做 strcat,因为 'stuff' 的第一个字符是空终止符,所以它会附加到正确的位置。
In C, you still need to initialize 'stuff', which can be done a couple of ways:
在 C 中,您仍然需要初始化“东西”,这可以通过以下几种方式完成:
char stuff[100]; // not initialized
stuff[0] = 'strcat(stuff, "hi ");
'; // first character is now the null terminator,
// so 'stuff' is effectively ""
strcpy(stuff, "hi "); // this initializes 'stuff' if it's not already.
回答by Keith Thompson
In the first case, stuffcontains garbage. strcatrequires both the destination and the source to contain proper null-terminated strings.
在第一种情况下,stuff包含垃圾。 strcat要求目标和源都包含正确的以空字符结尾的字符串。
char stuff[100];
stuff[0] = 'char stuff[100] = "";
'; /* ensures stuff contains a valid string */
strcat(stuff, "hi ");
strcat(stuff, "there");
will scan stufffor a terminating '\0'character, where it will start copying "hi ". If it doesn't find it, it will run off the end of the array, and arbitrarily bad things can happen (i.e., the behavior is undefined).
将扫描stuff终止'\0'字符,它将开始复制"hi ". 如果它没有找到它,它将从数组的末尾运行,并且可能会发生任意糟糕的事情(即,行为未定义)。
One way to avoid the problem is like this:
避免这个问题的一种方法是这样的:
char stuff[100] = "";
strcat(stuff,"hi ");
strcat(stuff,"there");
Or you can initialize stuffto an empty string:
或者您可以初始化stuff为空字符串:
char*
strncat(char *dest, const char *src, size_t n)
{
size_t dest_len = strlen(dest);
size_t i;
for (i = 0 ; i < n && src[i] != '##代码##' ; i++)
dest[dest_len + i] = src[i];
dest[dest_len + i] = '##代码##';
return dest;
}
which will fill all 100 bytes of stuffwith zeros (the increased clarity is probably worth any minor performance issue).
这将stuff用零填充所有 100 个字节(增加的清晰度可能值得任何轻微的性能问题)。
回答by shf301
Because stuffis uninitialized before the call to strcpy. After the declaration stuffisn't an empty string, it is uninitialized data.
因为stuff在调用之前未初始化strcpy。声明后stuff不是空字符串,就是未初始化的数据。
strcatappends data to the end of a string - that is it finds the null terminator in the string and adds characters after that. An uninitialized string isn't gauranteed to have a null terminator so strcatis likely to crash.
strcat将数据附加到字符串的末尾 - 即它在字符串中找到空终止符并在其后添加字符。未初始化的字符串不保证具有空终止符,因此strcat可能会崩溃。
If there were to intialize stuffas below you could perform the strcat's:
如果有stuff如下初始化,您可以执行 strcat 的:
回答by Mark Joseph Jorgensen
回答by sung
Strcat append a string to existing string. If the string array is empty, it is not going go find end of string ('\0') and it will cause run time error.
strcat 将字符串附加到现有字符串。如果字符串数组为空,则不会查找字符串的结尾 ( '\0') 并且会导致运行时错误。
According to Linux man page, simple strcat is implemented this way:
根据 Linux 手册页,简单的 strcat 是这样实现的:
##代码##As you can see in this implementation, strlen(dest)will not return correct string length unless destis initialized to correct c string values. You may get lucky to have an array with the first value of zero at char stuff[100];, but you should not rely on it.
正如您在此实现中所见,strlen(dest)除非dest初始化为正确的 c 字符串值,否则不会返回正确的字符串长度。您可能会很幸运地拥有一个第一个值为 0 的数组char stuff[100];,但您不应该依赖它。

