c-strcmp在一个简单的字符串搜索程序中返回__strcmp_sse2_unaligned()



我目前正在使用原始搜索例程。它使用strcmp将给定的字符串与两维字符串数组进行比较。GDP回报率:

&quot__strcmp_sse2_unaligned((位于/sysdeps/x86_64/multiarch/strcmp-se2-未对齐。S:30 30/sysdeps/x86_64/multiarch/strcmp-se2-unaligned。S:没有这样的文件或目录"。

编辑:试图继续,为字符串输入过程添加了命令行。不知怎么的,这是错误的。

这是我的代码

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char dictionary()
{

char **strings = (char**)malloc(5*sizeof(char*));
int i = 0;
for(i = 0; i < 5; i++){
//printf("%dn", i);
strings[i] = (char*)malloc(7*sizeof(char));
}
sprintf(strings[0], "mark");
sprintf(strings[1], "ala");
sprintf(strings[2], "wojtek");
sprintf(strings[3], "tom");
sprintf(strings[4], "john");

for(i = 0; i < 5; i++){
printf("Line #%d(length: %lu): %sn", i, strlen(strings[i]),strings[i]);
} 
for(i = 0; i < 5; i++){
free(strings[i]);
}
free(strings);
}
int cmp(char *s1, char *s2[][10]){
int i = 0;
//size_t l = strlen(s1);

for (i = 0; i < 5; i++){

if (strcmp(s1, s2[i][7*sizeof(char)]) == 0)

{

printf("OK n");

} else {

printf("sth is wrong n");  
}
return 0;
}
}
int main(){
char BufText[255];
int n=0;
char sign;
fflush(stdin);
n = 0;
do {
sign = getchar();
BufText[n ++] = sign;
if(n >= 253) break;
} while (sign !='n');
BufText [n] = 0;
char **dict = dictionary();
cmp(BufText, dict);
free_dictionary(dict);
return 0;
}                                                                                                                                    

正如评论中所说,您的代码中有很多缺陷。

首先,您尝试cmp("ala", dictionary);,但dictionary是一个未声明的变量。我认为您希望将dictionary()调用的结果用于cmp调用。因此,您需要将dictionary()结果存储到dictionary变量中。实际上无法完成,因为dictionary()函数不返回任何内容,并且在使用之前释放分配的dict。

我可以继续这样做,但这是您代码的修补版本。请随时要求澄清。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char **dictionary()
{
char **dict = (char**)malloc(sizeof(char*) * 5);
int i = 0;
for (i = 0; i < 5; i++)
dict[i] = (char*)malloc(sizeof(char) * 7);
sprintf(dict[0], "mark");
sprintf(dict[1], "ala");
sprintf(dict[2], "wojtek");
sprintf(dict[3], "tom");
sprintf(dict[4], "john");
for (i = 0; i < 5; i++)
printf("Line #%d(length: %lu): %sn", i, strlen(dict[i]),dict[i]);
return (dict);
}
void free_dictionary(char **dict)
{
for (int i = 0; i < 5; i++)
free(dict[i]);
free(dict);
}
void cmp(char *s1, char *s2[5])
{
int i = 0;
for (i = 0; i < 5; i++)
{
if (strcmp(s1, s2[i]) == 0)
printf("OK n");
else
printf("sth is wrong n");
}
}
int main()
{
char **dict = dictionary();
cmp("ala", dict);
free_dictionary(dict);
return (0);
}

最新更新