C 中的字符串比较给出分段错误
String comparison in C gives segmentation fault
我正在通过简单的练习教某人 C 编程。
我无法使 strcmp()
功能正常工作。
#include <stdio.h>
#include <string.h>
#define MAX 20
int main()
{
char s1[MAX], s2[MAX];
printf("Enter s1: ");
scanf("%s",s1);
printf("Enter s2: ");
scanf("%s",s2);
printf("S1 is %s\n",s1);
printf("S2 is %s\n",s2);
// string concatenation
strcat(s1,s2);
printf("S1 is %s\n",s1);
// string copy
strcpy(s1,s2);
printf("S1 is %s\n",s1);
// find the length of the string
int a = strlen(s1);
printf ("Length of S1 is %d\n", a);
int b = strlen(s2);
printf ("Length of S2 is %d\n", b);
// string comparison <<----- This is where it does not work
int c;
c = strcmp(s1, s2);
printf("C is %d\n",c);
if (c==0)
printf("S1 = S2\n");
else if (c<0)
printf("S1<S2\n");
else
printf("S1>S2\n");
return 0;
}
以上代码编译(有警告)但不执行。它抛出 segmentation fault
错误并退出。
我也使用了指针样式语法,但在编译过程中出现错误。
附带说明一下,我看到很多网站都使用 gets()
puts()
。但是当在我的程序中使用时,它告诉我不推荐使用上述功能。如何确定可以使用哪些功能以及在哪里寻找它们?
编辑
程序输出:
prasannarajaram@ubuntu:~/Documents/programs/C$ ./string
Enter s1: test
Enter s2: case
S1 is test
S2 is case
S1 is testcase
S1 is case
Length of S1 is 4
Length of S2 is 4
C is 0
S1 = S2
这是我开始添加 *
指针符号以尝试查看可行的地方。
只需删除行上的指针:
printf("S1 is %s\n",*s1);
printf("S2 is %s\n",*s2);
变成这样:
printf("S1 is %s\n",s1);
printf("S2 is %s\n",s2);
要了解更多关于c中指针的知识,网上有很多教程,比如这个:http://karwin.blogspot.com.br/2012/11/c-pointers-explained-really.html
要了解有关 c 和 c++ 函数的更多信息,您可以参考它们的官方文档,在此 link:http://en.cppreference.com/w/
编辑:
在这一行中,您正在制作一个 strcat:
strcat(s1,s2)
因此 s1 将具有 s1+s2 的值,但在下一行中,您要将 s2 复制到 s1 中。
strcpy(s1,s2)
之后,s1 将具有与 s2 相同的值,因此 s1 现在等于 s2。这就是为什么您的 strcmp 总是返回 0。您可以在输出中看到这种情况。
S1 is test #S1 initialy
S2 is case #S2 initialy
S1 is testcase #S1 after strcat(s1,s2)
S1 is case #S1 after strcpy(s1,s2)
可以看到最后S1和S2的值是一样的
这应该有效:)
您使用 scanf
不正确。我怀疑你的程序状态被调用 scanf
破坏了,但直到你调用 strcmp
.
才被发现
当使用 scanf
将标准输入读入字符串时,请使用此格式:
#define LENGTH 20
char str[LENGTH+1] = {0};
scanf("%20s", str); // note the "max-length" format parameter of 20.
通过使用 address-of 运算符 (&
) 意味着 scanf
将写入由传入的 value 定义的地址,如你没有为你的 s1
值设置一个初始值你的程序的行为是未定义的。
为了更安全,考虑在格式字符串中使用 LENGTH
定义的值,这样 20
的值就不会重复:
scanf("%" #LENGTH "s", str)
scanf
的第二个参数应该有一个地址,即s1
和s2
。将其更改为:
scanf("%s", s1);
scanf("%s", s2);
我正在通过简单的练习教某人 C 编程。
我无法使 strcmp()
功能正常工作。
#include <stdio.h>
#include <string.h>
#define MAX 20
int main()
{
char s1[MAX], s2[MAX];
printf("Enter s1: ");
scanf("%s",s1);
printf("Enter s2: ");
scanf("%s",s2);
printf("S1 is %s\n",s1);
printf("S2 is %s\n",s2);
// string concatenation
strcat(s1,s2);
printf("S1 is %s\n",s1);
// string copy
strcpy(s1,s2);
printf("S1 is %s\n",s1);
// find the length of the string
int a = strlen(s1);
printf ("Length of S1 is %d\n", a);
int b = strlen(s2);
printf ("Length of S2 is %d\n", b);
// string comparison <<----- This is where it does not work
int c;
c = strcmp(s1, s2);
printf("C is %d\n",c);
if (c==0)
printf("S1 = S2\n");
else if (c<0)
printf("S1<S2\n");
else
printf("S1>S2\n");
return 0;
}
以上代码编译(有警告)但不执行。它抛出 segmentation fault
错误并退出。
我也使用了指针样式语法,但在编译过程中出现错误。
附带说明一下,我看到很多网站都使用 gets()
puts()
。但是当在我的程序中使用时,它告诉我不推荐使用上述功能。如何确定可以使用哪些功能以及在哪里寻找它们?
编辑
程序输出:
prasannarajaram@ubuntu:~/Documents/programs/C$ ./string Enter s1: test Enter s2: case S1 is test S2 is case S1 is testcase S1 is case Length of S1 is 4 Length of S2 is 4 C is 0 S1 = S2
这是我开始添加 *
指针符号以尝试查看可行的地方。
只需删除行上的指针:
printf("S1 is %s\n",*s1);
printf("S2 is %s\n",*s2);
变成这样:
printf("S1 is %s\n",s1);
printf("S2 is %s\n",s2);
要了解更多关于c中指针的知识,网上有很多教程,比如这个:http://karwin.blogspot.com.br/2012/11/c-pointers-explained-really.html
要了解有关 c 和 c++ 函数的更多信息,您可以参考它们的官方文档,在此 link:http://en.cppreference.com/w/
编辑: 在这一行中,您正在制作一个 strcat:
strcat(s1,s2)
因此 s1 将具有 s1+s2 的值,但在下一行中,您要将 s2 复制到 s1 中。
strcpy(s1,s2)
之后,s1 将具有与 s2 相同的值,因此 s1 现在等于 s2。这就是为什么您的 strcmp 总是返回 0。您可以在输出中看到这种情况。
S1 is test #S1 initialy
S2 is case #S2 initialy
S1 is testcase #S1 after strcat(s1,s2)
S1 is case #S1 after strcpy(s1,s2)
可以看到最后S1和S2的值是一样的
这应该有效:)
您使用 scanf
不正确。我怀疑你的程序状态被调用 scanf
破坏了,但直到你调用 strcmp
.
当使用 scanf
将标准输入读入字符串时,请使用此格式:
#define LENGTH 20
char str[LENGTH+1] = {0};
scanf("%20s", str); // note the "max-length" format parameter of 20.
通过使用 address-of 运算符 (&
) 意味着 scanf
将写入由传入的 value 定义的地址,如你没有为你的 s1
值设置一个初始值你的程序的行为是未定义的。
为了更安全,考虑在格式字符串中使用 LENGTH
定义的值,这样 20
的值就不会重复:
scanf("%" #LENGTH "s", str)
scanf
的第二个参数应该有一个地址,即s1
和s2
。将其更改为:
scanf("%s", s1);
scanf("%s", s2);