错误:C未指定与字符串文字进行比较的结果
所以我试图用C来开个小玩笑,我不知道我做错了什么。我看到错误“Error:result of comparison of a string literal is unspecified”并且不知道如何修复它,有人能帮忙吗错误:C未指定与字符串文字进行比较的结果,c,string,cs50,C,String,Cs50,所以我试图用C来开个小玩笑,我不知道我做错了什么。我看到错误“Error:result of comparison of a string literal is unspecified”并且不知道如何修复它,有人能帮忙吗 #include<stdio.h> #include<cs50.h> int main(void){ string a = get_string("ENTER YOUR NAME FOR READING\n"); if (a == "da
#include<stdio.h>
#include<cs50.h>
int main(void){
string a = get_string("ENTER YOUR NAME FOR READING\n");
if (a == "david")
;
{
printf("...");
}
}
#包括
#包括
内部主(空){
string a=get_string(“输入您的姓名以便阅读”);
如果(a==“大卫”)
;
{
printf(“…”);
}
}
不能使用比较运算符比较c中的两个字符串。
试用
strcmp(const char* one, const char* two)
就是你的情况,
使用
#包括
#包括
内部主(空){
char[]a=get_string(“输入您的姓名以便阅读”);
char[]b=“大卫”;
如果(!strcmp(a,“david”))
{
printf(“…”);
}
}
此外,c中的字符串不声明为字符串。它们被声明为字符数组比较两个字符指针,如
char*a,*b;a==b编译器接受code>,但如果使用这种语法来比较字符串值,则可能无法得到预期的结果。有时它可能计算为true
,有时计算为false
。原因是,程序检查a
和b
是否指向同一地址,而不是检查它们是否具有相同的字符串值。考虑下面的程序。
#include <stdio.h>
int main()
{
char* a = "test";
char* b;
if ((b="test") == a)
printf("Impossible\n");
else
printf("Thought so\n");
printf("%p %p\n", a, b);
return 0;
}
从输出中可以看到,if
块的计算结果为true
。原因也在输出上a
和b
指向一个类似的地址。为什么?
它来自字符串池(编译器优化之一)。即使a
和b
在程序中的不同点初始化,因为它们引用的是相同的字符串常量,但编译器只初始化字符串常量一次
下面是可执行文件的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..
测试
仅初始化一次。为了快速检查,请考虑此程序的输出及其<代码> ObjDopp < /C> > < /P>
#include <stdio.h>
int main()
{
char* a = "test";
char* b;
if ((b="test1") == a)
printf("Impossible\n");
else
printf("Thought so\n");
printf("%p %p\n", a, b);
return 0;
}
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..
从输出中可以明显看出,a
和b
指向不同的位置,并且它们都具有不同的字符串值,由编译器分别初始化。那么,如果a
和b
具有相似的字符串值,那么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("Impossible\n");
else
printf("Thought so\n");
printf("%p %p\n", a, b);
return 0;
}
即使a
和b
具有相同的字符串值,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("Impossible\n");
else
printf("Thought so\n");
if (strcmp(b, a) == 0)
printf("Impossible\n");
else
printf("Thought so\n");
if (strcmp(b, c) == 0)
printf("Impossible\n");
else
printf("Thought so\n");
printf("%p %p %p\n", a, b, c);
return 0;
}
无论a
指向何处,b
和c
如果都具有相同的字符串值,则strcmp()
返回0
您的代码有三个问题:
1.
if(a==“david”)
a
衰减为指向数组a
的第一个元素的指针
“david”
是字符串文本
使用if
语句,您尝试将数组a
的地址与字符串literal“david”
的地址进行比较。字符串比较在C中不是这样工作的
使用strcmp()
-headerstring.h
比较字符串
strcmp()
如果字符串相等,则返回0。要使if
条件变为true
,您需要使用代码>否定运算符
2.
有一个错位的代码>介于if
条件和if
主体之间。移除它
3.
不推荐使用cs50.h
。如果不要求您明确使用它,请不要插手
#包括
#包括
内部主(空){
chara[20];
printf(“输入您的姓名以便阅读:\n”);
如果(!fgets(a,a的尺寸,标准尺寸))
{
fputs(“输入故障”,标准);
//进一步的错误例程。
}
a[strcspn(a,“\n”)]=0;
如果(!strcmp(a,“david”))
{
printf(“…”);
}
}
字符串在C中不是一流的数据类型,因此必须通过库调用来实现。Ref:您试图将“david”的地址与a的地址进行比较,因此它的计算结果将为false,同时还有一个wild代码>使得{}
块不是if
语句的一部分,请将其删除。使用cs50.h
应该是个笑话,对吧?我认为您颠倒了逻辑。在标识的情况下,strcmp返回0,这可能不是if中的预期效果。这是我的荣幸。char[]b=“david”代码>在您的代码中是完全冗余的,并且会使提问者感到困惑。char[]a=get_string(“输入您的姓名以便阅读”)代码>是一个错误。您应该声明一个指针,而不是数组:char*a=get_string(“输入您的名称以便读取”)
@Mohith您应该在问题的上下文中添加更多内容,并说明“david”实际上是什么,以及它返回地址的原因。@roberts-supportsmonicacellio我编辑了我的答案,请仔细阅读。我希望大家现在都很高兴:)“…被编译器接受,但是如果您使用这样的语法…”-这表明比较指针通常是一种不好的用法,这绝对不是事实。“地址a
是指向的,它是由malloc()创建的。”
属于可执行文件的堆部分,而b
指向的地址属于可执行文件的数据部分
$ 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..
#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("Impossible\n");
else
printf("Thought so\n");
printf("%p %p\n", a, b);
return 0;
}
Thought so
0x5607d9938260 0x5607d91fb004
#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("Impossible\n");
else
printf("Thought so\n");
if (strcmp(b, a) == 0)
printf("Impossible\n");
else
printf("Thought so\n");
if (strcmp(b, c) == 0)
printf("Impossible\n");
else
printf("Thought so\n");
printf("%p %p %p\n", a, b, c);
return 0;
}
Impossible
Impossible
Impossible
0x556debf41260 0x556deac28004 0x556deac28004
#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("...");
}
}