-1

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:

invalid conversion from char to 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(!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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    Note: Good, regular indentation makes many families of mistakes nearly impossible to commit. – user4581301 Jan 12 '23 at 20:41
  • @user4581301 i did not get you?? – Jasmeet Singh Jan 12 '23 at 20:43
  • 1
    Your code indentation was very very bad which made it hard to read. To make a code block type ``` press enter paste the code directly from your editor with no changes press enter type ``` and press enter again. – drescherjm Jan 12 '23 at 20:43
  • Many bugs are visible as irregularities in the code. If your code has few irregularities, these kinds of mistakes stand out. – user4581301 Jan 12 '23 at 20:44
  • @drescherjm is it ok now?? – Jasmeet Singh Jan 12 '23 at 20:46
  • `*ptr[i]` -- What is the result of doing this? Now, what is the result of doing this: `ptr[i]`? You could have also figured this out by doing `std::cout << *ptr[i] << "\n" << ptr[i];` to see the difference. – PaulMcKenzie Jan 12 '23 at 20:46
  • @PaulMcKenzie string comparison is happening in the loop?? – Jasmeet Singh Jan 12 '23 at 20:47
  • @JasmeetSingh -- The error `invalid conversion from char to const char *` basically tells you what you are doing wrong. `*ptr[i]` is a single character. – PaulMcKenzie Jan 12 '23 at 20:49
  • @PaulMcKenzie user4581301 it worked. i removed the dereferncing – Jasmeet Singh Jan 12 '23 at 20:49
  • @PaulMcKenzie so you mean when we write char *ptr[10] = {"books","television,"computer","sports"}; Here ptr is pointing to first character 'b' of books??? – Jasmeet Singh Jan 12 '23 at 20:51
  • Also this book doesn't look like good to learn C++ from scratch. Since it mostly uses C functions and constructs. That may be good when learning C++ with C knowledge, but useless otherwise. – sklott Jan 12 '23 at 20:52
  • @Jasmeet Singh I can surely say that the book is bad.:) – Vlad from Moscow Jan 12 '23 at 20:52
  • @sklott i have C knowledge but still can you suggest me a good C++ book bcz i have really found many errors in the example programs it has – Jasmeet Singh Jan 12 '23 at 20:53
  • @VladfromMoscow agree, any suggestions? – Jasmeet Singh Jan 12 '23 at 20:54
  • Take look at this: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – sklott Jan 12 '23 at 20:55
  • `so you mean when we write char *ptr[10]` -- That is a declaration. That is different than when you are executing the code. You are declaring that there are 10 `char *`. What do you do when you want to access each of them? You are mixing up the declaration with when you want to access them. – PaulMcKenzie Jan 12 '23 at 20:55

1 Answers1

2

strcmp() compares two null-terminated const char* strings. But, you are trying to compare a null-terminated char[] string to a single char, hence the error. This is because you are dereferencing the 2nd const char* string to access its 1st char element.

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:

if (strcmp(str, ptr[i]) == 0)
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Pay special attention to the `== 0`. It resolves a second mistake. `strcmp` returns 0 on a match, the distance between the two strings is zero. – user4581301 Jan 12 '23 at 20:48
  • 2
    I hate functions that return `0` on success, but I hate it even more when people use `!` instead of `== 0` when testing those functions for success. Even though it is functionally okay, it just doesn't read correctly in my mind. – Remy Lebeau Jan 12 '23 at 20:50
  • 1
    @RemyLebeau Right, because it pretends that the value is a boolean, *but* inverts the meanings of `true` and `false`. Both are bad on their own, in combination they are an abomination. – Konrad Rudolph Jan 12 '23 at 20:53
  • big thanks to @RemyLebeau for such a detailed comment and everyone here who jumped up on this problem so quickly. – Jasmeet Singh Jan 12 '23 at 20:59