简体   繁体   English

错误:从 char 到 const char * 的转换无效

[英]Error : Invalid conversion from char to const char *

I am facing this problem from day 1.我从第一天起就面临这个问题。

Actually, the book I am reading from has this program written in this way, but when I check it practically on Eclipse IDE, it does not work properly, always showing this error:实际上,我正在阅读的书上有这样写的程序,但是当我在 Eclipse IDE 上实际查看时,它不能正常工作,总是显示以下错误:

invalid conversion from char to const char *从 char 到 const char * 的无效转换

Although I know what the issue is, I don't know how to resolve this problem, and I am facing this problem with every program in which there is some string operation.虽然我知道问题出在哪里,但我不知道如何解决这个问题,而且我在每个有字符串操作的程序中都面临这个问题。

The error is with this if statement:这个if语句的错误是:

if(,strcmp(str, *ptr[i]))

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    const char *ptr[10] = { "books", "television", "computer", "sports" };
    int i = 0;
    char str[25];
    cout << "\nEnter your favorite leisure pursuit here :" << "\n";
    cin >> str;
    for (i = 0; i < 4; i++)
    {
        if (!strcmp(str, *ptr[i]))
        {
            cout << "\n your favorite pursuit is available here " << endl;
            break;
        }
    }
    if (i == 4)
    {
        cout << "\n\nYour favorite leisure is not available here" << endl;
    }
    return 0;
}

strcmp() compares two null-terminated const char* strings. strcmp()比较两个以 null 结尾的const char*字符串。 But, you are trying to compare a null-terminated char[] string to a single char , hence the error.但是,您正在尝试将以空字符结尾的char[]字符串与单个char进行比较,因此出现错误。 This is because you are dereferencing the 2nd const char* string to access its 1st char element.这是因为您取消引用第二个const char*字符串以访问其第一个char元素。

ptr is an array of const char* pointers, so you need to drop the extra * dereference operator when accessing ptr[i] , so that you compare the whole string, not just a single character of the string, eg: ptr是一个const char*指针数组,因此在访问ptr[i]时需要删除额外的*取消引用运算符,以便比较整个字符串,而不仅仅是字符串的单个字符,例如:

if (strcmp(str, ptr[i]) == 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:从char到const char的无效转换* - error: invalid conversion from char to const char* 错误:从&#39;char&#39;到&#39;const char&#39;的无效转换 - ERROR: Invalid conversion from 'char' to 'const char' 错误:从&#39;char&#39;到&#39;const char *&#39;的无效转换 - error: invalid conversion from 'char' to 'const char*' 错误:从&#39;char&#39;到&#39;const char *&#39;的无效转换 - Error: invalid conversion from 'char' to 'const char*' 错误:从&#39;char&#39;到&#39;const char *的无效转换 - error: invalid conversion from ‘char’ to ‘const char* 从“const char**”到“char* const*”的无效转换 - Invalid conversion from 'const char**' to 'char* const*' 错误:从&#39;int&#39;到&#39;const char *&#39;的无效转换 - error: invalid conversion from ‘int’ to ‘const char*’ 错误:从“int”到“const char*”的无效转换 - Error: Invalid conversion from 'int' to 'const char*' 奇怪的错误,错误:从&#39;const char *&#39;到&#39;char&#39;的无效转换[-fpermissive] - Weird Error, error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] 在构造函数&#39;Player :: Player()&#39;中:错误:从&#39;const char *&#39;到&#39;char&#39;的无效转换 - In constructor ‘Player::Player()’: error: invalid conversion from ‘const char*’ to ‘char’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM