错误:未为 C 指定与字符串文本的比较结果



所以我试图用C开一个小玩笑,我不知道我做错了什么。我看到错误">错误:未指定与字符串文本进行比较的结果",不知道如何解决它,有人可以帮忙。

#include<stdio.h>
#include<cs50.h>
int main(void){
string a = get_string("ENTER YOUR NAME FOR READINGn");
if (a == "david")
;
{
printf("...");
}
}

您的代码有三个问题:

1.

if (a == "david")

a衰减到指向数组a的第一个元素的指针。

"david"是字符串文本。

使用if语句,尝试将数组a的地址与字符串文本"david"的地址进行比较。字符串比较模具在 C 中不是这样工作的。

使用strcmp()- 标头string.h来比较字符串。

如果字符串相等,则strcmp()返回 0。要使if条件变为true您需要使用否定运算符!

阿拉伯数字。

if状况和身体之间if;错位。删除它。

3.

不推荐使用cs50.h。如果您不需要明确使用它,请不要动手。


#include <stdio.h>
#include <string.h>
int main (void) {
char a[20];
printf("ENTER YOUR NAME FOR READING:n");
if (!fgets(a, sizeof a, stdin))
{
fputs("Failure at input", stderr);
// further Error routine.
}
a[strcspn(a, "n")] = 0;
if (!strcmp(a,"david"))
{
printf("...");
}
}

不能使用比较运算符比较 c 中的两个字符串。 尝试使用

strcmp(const char* one, const char* two)

也就是说,在您的情况下 用

#include<stdio.h>
#include<cs50.h>
int main(void){
char[] a = get_string("ENTER YOUR NAME FOR READINGn");
char[] b = "david";
if (!strcmp(a,"david"))
{
printf("...");
}
}

此外,c 中的字符串不会声明为字符串。它们被声明为 char 数组

编译器接受像这样比较两个字符指针char *a, *b; a == b;,但如果使用这样的语法来比较字符串值,则可能无法获得预期的结果。有时它可能会评估为true,有时可能评估为false。原因是,程序检查ab是否指向相同的地址,而不是检查它们是否具有相同的字符串值。考虑下面的程序。

#include <stdio.h>
int main()
{
char* a = "test";
char* b;
if ((b="test") == a)
printf("Impossiblen");
else
printf("Thought son"); 
printf("%p %pn", a, b);
return 0;
}

输出

Impossible
0x55b6bd52d004 0x55b6bd52d004

从输出中看到,if块的计算结果为true。原因也在于输出;ab指向一个类似的地址。为什么?

它来自字符串池,这是编译器优化之一。即使ab在程序的不同点初始化,因为它们引用相同的字符串常量,字符串常量也仅由编译器初始化一次。

这是可执行文件的objdump

$ objdump -s -j .rodata 
a.out:     file format elf64-x86-64
Contents of section .rodata:
2000 01000200 74657374 00496d70 6f737369  ....test.Impossi
2010 626c6500 54686f75 67687420 736f0025  ble.Thought so.%
2020 70202570 0a00                        p %p..          

test仅初始化一次。要进行快速检查,请考虑此程序的输出及其objdump

#include <stdio.h>
int main()
{
char* a = "test";
char* b;
if ((b="test1") == a)
printf("Impossiblen");
else
printf("Thought son"); 
printf("%p %pn", a, b);
return 0;
}

输出

Thought so
0x557a96aa9004 0x557a96aa9009

objdump

$ objdump  -s -j .rodata a.out  
a.out:     file format elf64-x86-64
Contents of section .rodata:
2000 01000200 74657374 00746573 74310049  ....test.test1.I
2010 6d706f73 7369626c 65005468 6f756768  mpossible.Though
2020 7420736f 00257020 25700a00           t so.%p %p..

从输出中可以明显看出,ab指向不同的位置,并且两者都具有由编译器单独初始化的不同字符串值。那么,如果ab具有相似的字符串值,a==b是否总是计算为 true?请考虑以下程序。

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
int main()
{
char* a = malloc(sizeof(char) * 5);
strcpy(a, "test");
char* b;
if ((b="test") == a)
printf("Impossiblen");
else
printf("Thought son"); 
printf("%p %pn", a, b);
return 0;
}

输出

Thought so
0x5607d9938260 0x5607d91fb004

即使ab具有相同的字符串值,if计算结果为false。为什么?a指向的地址,由malloc()创建,属于可执行文件的堆部分,而b指向的地址属于可执行文件的数据部分,它们确实不同。

那么我们应该怎么做才能比较字符串值呢?

嗯,这很简单,已经有库函数可以执行此任务。您可以将它们与string.h.请考虑以下代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
int main()
{
char* a = malloc(sizeof(char) * 5);
strcpy(a, "test");
char* b = "test";
char* c;
if (strcmp((c="test"), a) == 0)
printf("Impossiblen");
else
printf("Thought son"); 
if (strcmp(b, a) == 0)
printf("Impossiblen");
else
printf("Thought son"); 
if (strcmp(b, c) == 0)
printf("Impossiblen");
else
printf("Thought son"); 
printf("%p %p %pn", a, b, c);
return 0;
}

输出

Impossible
Impossible
Impossible
0x556debf41260 0x556deac28004 0x556deac28004 

无论abc指向何处,如果它们都具有相同的字符串值,则strcmp()返回0

最新更新