简体   繁体   English

C ++从char到const char的无效转换*

[英]C++ invalid conversion from char to const char*

How do I fix this function? 我该如何修复这个功能? Error I'm getting is: invalid conversion from char to const char* on line 我得到的错误是:从char到char char *的无效转换

if(strcmp('\0', p->pubLow[i][j]) == 0) {

Code: 码:

struct Prefix {
    int  no;             // number of entries
    int  area[MAX];      // area elements
    char pubLow[MAX][8]; // low end of publisher range
    char pubHgh[MAX][8]; // high end of publisher range
    int  pubLen[MAX];    // no of chars in publisher string
};

int minNoDigits(const Prefix* p, int area) {
    int minNoDigits = 0;
    for(int i = 0; i < p->no; i++) {
        if(area == p->area[i]) {
            cout << "p->pubLow[i] is: " << p->pubLow[i] << endl;
            minNoDigits = 1;
            for(int j = 1; j < 3; j++) {
                if(p->pubLow[i][j] = '\0') {
                    j = 3;
                    i = p->no;
                }
                else {
                    minNoDigits++;
                }
            }
        }
    }
    return minNoDigits;
}

Edit to provide more information. 编辑以提供更多信息。

I've changed the line to 我把线路改成了

if(p->pubLow[i][j] = '\0') {

New error: 新错误:

assignment of read-only location p->Prefix::pubLow[i][j]

I understand p->pubLow[i][j] is actually a char , because pubLow seems from your code to be a char** so this is what you are looking for: 我理解p->pubLow[i][j]实际上是一个char ,因为pubLow似乎从你的代码中变成了char**所以这就是你要找的东西:

if(p->pubLow[i][j] == '\0') {

strcmp() has the following prototype: strcmp()有以下原型:

int strcmp ( const char * str1, const char * str2 );

Here is the documentation . 这是文档

So, to summarize, you are trying to compare 2 charaters and strcmp() is designed to compare 2 constant strings . 因此,总而言之,您正在尝试比较2 个字符,strcmp()旨在比较2个常量字符串

strcmp比较来自const char* s的字符串:

if(strcmp("", p->pubLow[i][j]) == 0) {

Why do you need strcmp(...) for char comparsion? 你为什么需要strcmp(...)进行char comparsion? You can do just 你可以做到

if('\0'== p->pubLow[i][j]) ...

'\\0' is a char not a string like other have replied. '\\0'是一个char而不是像其他人一样回复的字符串。

I'm not sure what you want tot achieve with if(strcmp('\\0', p->pubLow[i][j]) == 0) 我不确定你想用if(strcmp('\\0', p->pubLow[i][j]) == 0)实现什么if(strcmp('\\0', p->pubLow[i][j]) == 0)

If you want to check end of string you can do this if('\\0' == p->pubLow[i][j]) 如果你想检查字符串的结尾,你可以这样做if('\\0' == p->pubLow[i][j])

It looks like p->pubLow[i] is a char* and you're trying to compare it character-by-character with the termination character \\0 ? 它看起来像p->pubLow[i]是一个char* ,你试图将它逐个字符与终止字符\\0

As others have pointed out, strcmp is meant to be used to compare strings not individual characters. 正如其他人所指出的那样, strcmp用于比较字符串而不是单个字符。 You could use == operator to compare the characters instead, but since you're doing this to find the length of the string, it would be better to do this directly. 您可以使用==运算符来比较字符,但由于您这样做是为了找到字符串的长度,因此最好直接执行此操作。

You want to return the number of digits in the p->pubLow[i] with the same area as area , right? 你想要返回p->pubLow[i]与区域area相同的位数, p->pubLow[i]吧? How about this: 这个怎么样:

int minNoDigits(const Prefix* p, int area) {
    for(int i = 0; i < p->no; i++) {
        if(area == p->area[i]) {
            cout << "p->pubLow[i] is: " << p->pubLow[i] << endl;
            return strlen(pubLow[i]) + 1;
        }
    }
    return 0;
}

You might also find break useful in future - it will break you out of the for loops (I notice you were setting j and i directly to do this). 您可能还会发现将来有用的break - 它会让您脱离for循环(我注意到您正在设置ji直接执行此操作)。

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

相关问题 C ++从&#39;char *&#39;到&#39;const unsigned char *&#39;的无效转换 - C++ invalid conversion from ‘char*’ to ‘const unsigned char*’ (C++) 错误:从 'char' 到 'const char*' 的无效转换 [-fpermissive] - (C++) error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] C ++从&#39;const char *&#39;到&#39;char *&#39;的无效转换 - c++ invalid conversion from 'const char*' to 'char*' C ++从&#39;char&#39;到&#39;const char&#39;的无效转换 - C++ invalid conversion from 'char' to 'const char' C ++从&#39;const char *&#39;到&#39;char *&#39;Arduino Uno的无效转换 - C++ invalid conversion from 'const char*' to 'char*' Arduino Uno C ++错误:从'char'到'const char *'的转换无效 - C++ Error: Invalid conversion from 'char' to 'const char*' c ++“错误:从&#39;const char *&#39;到&#39;char *&#39;的无效转换” - c++ “error: invalid conversion from ‘const char*’ to ‘char*’” C ++,从&#39;char&#39;到&#39;const char *&#39;的转换无效 - C++, Invalid conversion from ‘char’ to ‘const char*’ 从 'char' 到 'const char*' 的无效转换 c++ - invalid conversion from ‘char’ to ‘const char*’ c++ C ++从&#39;char&#39;到&#39;const char *&#39;的无效转换[-fpermissive] - C++ invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM