为什么我不能在 strcmp() 中使用搭配?
Why can not I use collocations in the strcmp()?
以下代码不起作用:
char text[10];
scanf(" %s", &text);
if (strcmp(text, "some text") == 0)
puts("some text");
但是当我这样更改'if part'时,一切都很好:
if (strcmp(text, "sometext") == 0)
为什么会这样,如何解决?
问题不在于您的 strcmp
通话,而在于您的 scanf
通话!当您键入 some text
作为输入时,scanf
将在第一个空格处停止读取,因此您的 text
字符串将为 "some" - 因此,strcmp
调用将(正确地)return 非零,因为这是 not 与 "some text".
相同
有关 scanf
、空格和 %s
格式的此功能的详细信息,请参阅此主题:How do you allow spaces to be entered using scanf?
调用 fgets 读取白色 space 并将字符串与 strncmp 进行比较:
fgets (text, 100, stdin);
puts(text);
if (strncmp(text, "some text",9) == 0)
puts("some text");
我的回答是在不询问论坛问题的情况下对此类问题进行排序。我会花 2-3 分钟看看那里发生了什么:
int main()
{
char text[10];
int result;
result = scanf(" %s", &text);
printf("scanf result: %d, scanned text: \"%s\"\n", result, text);
if (strcmp(text, "some text") == 0)
puts("some text");
}
调试 - 即找到问题的根源与编程知识同样重要。如果你自己添加这一行,你很快就会自己解决这个问题。
题外话,但评论太长了。
你的 scanf
行实际上是错误的(不是指空格),你只是很幸运它在这里工作。您需要删除 text
前面的符号 &
。您将 text
的地址传递给 scanf
,这实际上是指向 char[10]
的指针。对于 scanf(" %s", text);
、text
"decays" 指向其第一个元素 (char*
) 的指针,这正是您真正想要的。在这里,恰好数组的地址 (&text
) 与数组中第一个元素的地址 (&(text[0])
,或 *(text + 0)
) 相同,因为它们'两者都在同一个地方自动存储。例如,如果您有类似
char* text = malloc(10);
scanf(" %s", &text);
您将通过覆盖从指向您的 malloc
ed 内存的指针地址开始的内存来调用未定义的行为。在这里,文本的地址是指向自动存储中指针的地址,而它实际指向的内存在其他地方。您可以在以下程序中看到这一点。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char text[10];
char* text2 = malloc(10);
printf("address of text = %p\n", (void*)(&text)); // same address as below
printf("address of text[0] = %p\n", (void*)(&(text[0]))); // same address as above
printf("address of text2 = %p\n", (void*)&text2); // different than below
printf("address of text2[0] = %p\n", (void*)(&(text2[0]))); // different than above
return 0;
}
&text == &(text[0])
表示数组的地址和数组首元素的地址是等价的,但是&text2 != &(text2[0])
表示指针的地址不等于数组的地址您 malloc
编辑的记忆的第一个元素。
以下代码不起作用:
char text[10];
scanf(" %s", &text);
if (strcmp(text, "some text") == 0)
puts("some text");
但是当我这样更改'if part'时,一切都很好:
if (strcmp(text, "sometext") == 0)
为什么会这样,如何解决?
问题不在于您的 strcmp
通话,而在于您的 scanf
通话!当您键入 some text
作为输入时,scanf
将在第一个空格处停止读取,因此您的 text
字符串将为 "some" - 因此,strcmp
调用将(正确地)return 非零,因为这是 not 与 "some text".
有关 scanf
、空格和 %s
格式的此功能的详细信息,请参阅此主题:How do you allow spaces to be entered using scanf?
调用 fgets 读取白色 space 并将字符串与 strncmp 进行比较:
fgets (text, 100, stdin);
puts(text);
if (strncmp(text, "some text",9) == 0)
puts("some text");
我的回答是在不询问论坛问题的情况下对此类问题进行排序。我会花 2-3 分钟看看那里发生了什么:
int main()
{
char text[10];
int result;
result = scanf(" %s", &text);
printf("scanf result: %d, scanned text: \"%s\"\n", result, text);
if (strcmp(text, "some text") == 0)
puts("some text");
}
调试 - 即找到问题的根源与编程知识同样重要。如果你自己添加这一行,你很快就会自己解决这个问题。
题外话,但评论太长了。
你的 scanf
行实际上是错误的(不是指空格),你只是很幸运它在这里工作。您需要删除 text
前面的符号 &
。您将 text
的地址传递给 scanf
,这实际上是指向 char[10]
的指针。对于 scanf(" %s", text);
、text
"decays" 指向其第一个元素 (char*
) 的指针,这正是您真正想要的。在这里,恰好数组的地址 (&text
) 与数组中第一个元素的地址 (&(text[0])
,或 *(text + 0)
) 相同,因为它们'两者都在同一个地方自动存储。例如,如果您有类似
char* text = malloc(10);
scanf(" %s", &text);
您将通过覆盖从指向您的 malloc
ed 内存的指针地址开始的内存来调用未定义的行为。在这里,文本的地址是指向自动存储中指针的地址,而它实际指向的内存在其他地方。您可以在以下程序中看到这一点。
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char text[10];
char* text2 = malloc(10);
printf("address of text = %p\n", (void*)(&text)); // same address as below
printf("address of text[0] = %p\n", (void*)(&(text[0]))); // same address as above
printf("address of text2 = %p\n", (void*)&text2); // different than below
printf("address of text2[0] = %p\n", (void*)(&(text2[0]))); // different than above
return 0;
}
&text == &(text[0])
表示数组的地址和数组首元素的地址是等价的,但是&text2 != &(text2[0])
表示指针的地址不等于数组的地址您 malloc
编辑的记忆的第一个元素。