0
 # include <iostream>
# include <string.h>
using namespace std;
int main()
{
    int a=10;
    int b=20;
    char op[10];
    const char p='+';
    cout<<"enter the operation"<<endl;
    cin>>op;
    if(!strcmp(op,p)==0)
{
        cout<<a+b;
}
    return 0;
}

compilation result

12 17 C:\Users\DELL\Documents\cac.cpp [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]

I am a beginner. Please tell me what mistake have I done.

  • compare `op[0]==p` you'll compare chars it will work. – Jean-François Fabre Oct 05 '16 at 18:32
  • 3
    as a beginner you should probably... 1) completely forget about `using namespace std;` 2) use `std::string` instead of `char[]` 3) read a book about some basics (see e.g. [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)) – 463035818_is_not_an_ai Oct 05 '16 at 18:33
  • thank you. but can you tell why do we use op[0]? – rahul gupta Oct 05 '16 at 18:42
  • 1
    strcmp takes two pointers to character string, it doesn't take a single char as a parameter. here it is not problem of const. – Raindrop7 Oct 05 '16 at 18:44
  • Read the error message more carefully. The issue isn't `char` vs. `const char`; it's `char` vs. `const char*` -- the `*` is important. – Pete Becker Oct 05 '16 at 19:48
  • Does this answer your question? [Difference between char\* and const char\*?](https://stackoverflow.com/questions/9834067/difference-between-char-and-const-char) – Sampath Dec 18 '19 at 10:52

2 Answers2

2

This isn't about the difference between char and const char, but between char [] and char.

strcmp expects two character arrays.

op is an array of (10) characters. Good: that's what strcmp expects.

p is a single character. Not good: strcmp needs a char array, and p isn't any kind of array, but a single character.

You can change p from a single char '+' to a char array "+", or compare only the 0th character of op, as suggested in a comment above.

Topological Sort
  • 2,733
  • 2
  • 27
  • 54
0

there is no version of strcmp that takes a single character as a parameter but instead it takes two string and compares them.

if you want to compare a single char variable with a string you can compare it with the first element of string or with any other element:

#include <iostream>
#include <string>


int main()
{

    char op[10]  = "Hi";
    const char p = '+';

   // if( strcmp( op, p) ) // error cannot covert parameter 2 from char to const char*
   //    cout << "op and p are identic" << std::endl;
   // else
   //    std::cout << "op and b are not identic" << std::endl;

    if(op[0] == p)
        std::cout << "op[0] and p are identic" << std::endl;
    else
        std::cout << "op[0] and p are not identic" << std::endl;

    const char* const pStr  = "Bye"; //constant pointer to constant character string: pStr cannot change neither the address nor the value in address
    const char* const pStr2 = "bye"; // the same as above

    // pStr++; //error
    // pStr[0]++; // error 


    if( !strcmp( pStr, pStr2) )
        std::cout << "pStr and pStr2 are identic" << std::endl;
    else
        std::cout << "pStr and pStr2 are Not identic" << std::endl;

    return 0;
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
  • The `char*` to a string constant is a bit dangerous. See [here](http://coliru.stacked-crooked.com/a/5fb7b70d30188233), and [here](http://stackoverflow.com/q/20944784) – wally Oct 05 '16 at 19:12
  • @flatmouse I know it's so dangerous. my example's focus is only on strcmp. thanx – Raindrop7 Oct 05 '16 at 19:35
  • thanx guys. But what really is the difference between char, char*, const char, const char* – rahul gupta Oct 07 '16 at 17:01