strcmp - ‘[’标记之前的预期主表达式
strcmp - expected primary-expression before ‘[’ token
在我的主要代码中,我有一个操作,我想将字符串“word[i]”的某个字符与拉丁字母“b”进行比较。显然我尝试使用“ word[i] == “b” ”但是出现了错误。经过一些研究,我发现在 C++ 中,“word[i] == "b"”比较两个指针。有人建议应该改用 strcmp()。
所以我使用了 strcmp() 但仍然出现错误。有人可以向我解释为什么它不起作用吗?
最小工作示例:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string test;
cin >> test;
if(strcmp(string[0], "a") == 0){
cout << "yes";
}
}
-->
untitled.cpp: In function ‘int main()’:
untitled.cpp:8:18: error: expected primary-expression before ‘[’ token
8 | if(strcmp(string[0], "a") == 0){
| ^
Compilation failed.
你的字符串不叫string
;它被称为 test
.
您通过提供类型名称而不是表达式来混淆编译器。
修复:
if(strcmp(test[0], "a") == 0){
// ^^^^
然而,这仍然是错误的。 test[0]
不是字符串;它是一个字符。
解决方案:
if (test[0] == 'a') {
// ^ ^
After a bit of research i found out that in C++ " word[i] == "b" " compares to pointers
对于 C-strings,当然可以,但是在 C++ 中,std::string
具有您所期望的 ==
定义。
string[0]
无效,因为 string
是类型名称。看来你的意思是 test[0]
.
另请注意,strcmp(test[0], "a") == 0
将不起作用,因为 test[0]
是 char
,而 strcmp()
需要指向 C-style 字符串(以'[=17=]'
) 作为两个参数。
你想比较字符,而不是字符串,所以应该是test[0] == 'a'
或test[0] == "a"[0]
。
Obviously i tried to use " word[i] == "b" " but got an error
表达式 word[i]
(根据所提供的程序,您的意思似乎是 test[i] == "b"
或更准确地说 test[0] == "a"
)具有类型 char
而字符串文字 "b"
的类型为 const char *
。所以编译器会报错,因为类型不兼容。
函数strcmp
是用来比较两个c-strings的。如果你会写例如
strcmp( word[i], "b" ) == 0
然后你又会得到一个数组,因为表达式 word]i]
不是指向字符串的指针。它产生一个字符。
而且在这个 if 语句中
if(strcmp(string[0], "a") == 0){
您正在使用类型说明符 std::string
而不是对象。
你只需要写
if ( test[0] == 'a' ){
即比较了两个 char
类型的对象:对象 test[0]
和字符文字 '0'
.
在我的主要代码中,我有一个操作,我想将字符串“word[i]”的某个字符与拉丁字母“b”进行比较。显然我尝试使用“ word[i] == “b” ”但是出现了错误。经过一些研究,我发现在 C++ 中,“word[i] == "b"”比较两个指针。有人建议应该改用 strcmp()。
所以我使用了 strcmp() 但仍然出现错误。有人可以向我解释为什么它不起作用吗?
最小工作示例:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string test;
cin >> test;
if(strcmp(string[0], "a") == 0){
cout << "yes";
}
}
-->
untitled.cpp: In function ‘int main()’:
untitled.cpp:8:18: error: expected primary-expression before ‘[’ token
8 | if(strcmp(string[0], "a") == 0){
| ^
Compilation failed.
你的字符串不叫string
;它被称为 test
.
您通过提供类型名称而不是表达式来混淆编译器。
修复:
if(strcmp(test[0], "a") == 0){
// ^^^^
然而,这仍然是错误的。 test[0]
不是字符串;它是一个字符。
解决方案:
if (test[0] == 'a') {
// ^ ^
After a bit of research i found out that in C++ " word[i] == "b" " compares to pointers
对于 C-strings,当然可以,但是在 C++ 中,std::string
具有您所期望的 ==
定义。
string[0]
无效,因为 string
是类型名称。看来你的意思是 test[0]
.
另请注意,strcmp(test[0], "a") == 0
将不起作用,因为 test[0]
是 char
,而 strcmp()
需要指向 C-style 字符串(以'[=17=]'
) 作为两个参数。
你想比较字符,而不是字符串,所以应该是test[0] == 'a'
或test[0] == "a"[0]
。
Obviously i tried to use " word[i] == "b" " but got an error
表达式 word[i]
(根据所提供的程序,您的意思似乎是 test[i] == "b"
或更准确地说 test[0] == "a"
)具有类型 char
而字符串文字 "b"
的类型为 const char *
。所以编译器会报错,因为类型不兼容。
函数strcmp
是用来比较两个c-strings的。如果你会写例如
strcmp( word[i], "b" ) == 0
然后你又会得到一个数组,因为表达式 word]i]
不是指向字符串的指针。它产生一个字符。
而且在这个 if 语句中
if(strcmp(string[0], "a") == 0){
您正在使用类型说明符 std::string
而不是对象。
你只需要写
if ( test[0] == 'a' ){
即比较了两个 char
类型的对象:对象 test[0]
和字符文字 '0'
.