连接空终止符会导致分段错误
我是 C 新手,最近我遇到了 strcat 函数的问题。 我有一个参数数组,定义如下:
#define MAXARGS 10;
char *argvalues[MAXARGS];
我想要的是将数组的最后一个非空元素与空终止符连接起来。这是我的代码片段:
while (argvalues[i] != NULL) {
if (argvalues[i] == NULL){
strcat(argvalues[i-1], '/0');
printf("i is: %d\n", i);
break;
}
i++;
}
您知道为什么会发生分段错误吗?这实际上是使用 strcat
的正确方法吗?
i am new to C and recently i have a problem with a strcat function.
I have an array of arguments defined like:
#define MAXARGS 10;
char *argvalues[MAXARGS];
All i want is to concatenate the last non-null element of the array with a null terminator. Here is the piece of my code for that:
while (argvalues[i] != NULL) {
if (argvalues[i] == NULL){
strcat(argvalues[i-1], '/0');
printf("i is: %d\n", i);
break;
}
i++;
}
Do you have any idea why segmentation fault happens and is it actually the right way of using strcat
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
谢谢大家的回复!!!!
我想我解决了这个问题,主要是我所做的是创建一个新的 char[size] 变量,然后复制其中的最后一个参数,将空终止符放入该变量中,然后复制回最后一个参数。工作正常,但代码很乱。所以以后可能会用它来做某事。
strcat 仅适用于字符串,不适用于字符,感谢您指出我的代码中的所有缺陷!
谢谢大家!!!!
非常感谢!
Thank you all for the replies!!!!!
i think i solved the problem, mainly what i have done is to create a new char[size] variable, then copy the last argument in it, put the null terminator in that variable and then copy back to the last argument. works ok but the code is messy. so might do smth with it later on.
strcat is for strings only, not for chars, thanks for pointing out all the lacks in my code!
Thanks everyone!!!!
much appreciated!
您的循环超出了数组的末尾。这就是导致段错误的原因。在 C 中,不能保证未初始化的变量将为 NULL,或任何其他与此相关的变量。因此,您的循环将继续循环,直到它尝试访问不存在的 argvalues[11]。因此分段错误。
这两者是互相矛盾的。它甚至永远不会到达 if 语句,因为当 while 循环发现 argvalues[i] == NULL 时,它会首先退出。如果所有 10 个参数都已设置,那么您将尝试访问 argvalues[11],这将出现前面提到的段错误。
要正确使用 strcat,您必须有一个足够大的字符串缓冲区,以接受要连接到其末尾的字符串。例如,如果要将“world”添加到已包含“hello”的缓冲区,则必须将 hello 缓冲区声明为至少与“hello”+“world”加上 1 个字符(“\0”在最后)。
如果您尝试这样做,它将失败:
为了准确地弄清楚您在做什么,我们需要更多的描述和代码。将空终止符添加到参数的最佳位置是在首次设置参数时。根据你如何设置你的论点,它可能已经发生在你身上。
Your loop is wandering past the end of your array. That's what's causing the segfault. In C there is no guarantee that an uninitialized variable will be NULL, or anything else for that matter. So your loop keeps looping until it tries to access argvalues[11], which doesn't exist. Hence segmentation fault.
These two contradict each other. It will never even reach the if statement because the while loop will exit first when it discovers that argvalues[i] == NULL. And if all 10 of your arguments are set then you will attempt to access argvalues[11] which will segfault as previously mentioned.
To properly use strcat you have to have a string buffer that's large enough to accept the string you are concatenating on to the end of it. For example, if you want to add " world" on to a buffer already containing "hello" the hello buffer must be declared to be at least as long as "hello"+" world" plus 1 more character (the '\0' at the end).
If you try to do this, it will fail:
To figure out exactly what you're doing we'll need a little more description and code. The best place to add the null terminator to your arguments would be when the arguments are first set. Depending on how you're setting your arguments it may already be happening for you.
正如 Alcon 指出的那样,使用这段代码你永远不会到达 strcat() 。您需要将循环更改为如下所示:
此外,将空指针传递给 strcat()。它需要一个字符串,而不是一个字符。 '\0' 是一个空字符,它将被“提升”为空指针。 “”或“\0”将是一个空字符串,但这也不会添加空值。 strcat(0) 在这里使用不正确,因为它在连接之前查找空终止符。因此,您不能使用它来添加空终止符!
如果数组中的每个字符串还没有空终止符,或者不知道其长度,我不知道如何找到添加空终止符的末尾。这似乎有点像“给我打电话,我会告诉你我的电话号码”的情况。
As Alcon pointed out, you'll never reach the strcat() with this code. You need to change your loop to something like this:
Also, passing a null pointer to strcat(). It takes a string, not a character. '\0' is a null character, which will get "promoted" to a null pointer. "" or "\0" would be an empty string, but that wouldn't add a null either. strcat(0) is not the right thing to use here, because it looks for the null terminator before concatenating. Therefore, you can't use it to add a null terminator!
If you don't already have a null terminator on each string in the array, or otherwise know its length, I don't see how you can find the end to add a null terminator. It seems like sort of a "call me and I'll tell you my phone number" situation.
strcat 像这样连接两个 char 指针。
pi 现在成立 - 你好世界
您必须确保目的地有足够的空间来容纳串联结果。
strcat concatenates two char pointers like so.
pi now holds - hello world
You have to make sure that the destination has enough room for the concatenated result.
这不是这样做的好方法!
如果您确定您的 char* 没有“\0”,您可以这样做:
(oldargvalues 是为了跟踪 char* 的开头)。
但尽管如此,这很奇怪,因为如果你已经有一个“\0”字符,循环就会结束。
This is not the good way to do that!
If you are sure that your char* has no "\0" you could do your stuff like this:
(oldargvalues is to keep a track of the beginning of the char*).
But even though, it's strange since the loop will end if you have already an "\0" char.
我不明白你的循环。看起来它永远不会做任何事情。在 while 循环中检查 argvalues[i] != NULL 是否。在下一个语句中,您检查它是否为 NULL。其中之一总是为假(它不能同时为 NULL 和非 NULL),因此这个循环永远不会执行任何操作。我想你想要类似下面的东西:
I don't understand the loop you have. It looks like it will never do anything. In the while loop you check if argvalues[i] != NULL. In the very next statement, you check if it is NULL. One of those is always going to be false (it can't be both NULL and non-NULL), so this loop will never do anything. I think you want something like the following:
Alcon 和 Fred 所说的是真的,而且看起来您的 argvalues 已经以 null 终止,因此不清楚您真正想要做什么。
除非第一个参数字符串位于您已分配的缓冲区中(因此您知道它足够大),否则不应使用 strcat。就像如果你有
void main(char* argv[], int argc)
你永远不会说strcat(argv[i], ...)
,即使>strcat(..., "")
是一个无操作。What Alcon and Fred said are true, plus it looks like your
argvalues
are already null-terminated, so it's not clear what you're really trying to do.You shouldn't use
strcat
unless the first argument string is in a buffer that you've allocated so you know it's big enough. Like if you havevoid main(char* argv[], int argc)
you would never saystrcat(argv[i], ...)
, even thoughstrcat(..., "")
is a no-op.几个问题:
您的 argvalues 数组不会自动初始化为全部 NULL,您必须手动执行此操作,因此在蝙蝠的右边,您无法像其他人指出的那样检测 while() 循环中的 NULL。
argvalues 是如何设置的?
Couple of problems:
Your argvalues array is not automatically initialized to be all NULL, you'll have to do that by hand, so right of the bat, you can't detect the NULL in your while() loop as others have indicated.
How is argvalues being set?