c++ - I'm getting invalid conversion from char to const char error.How to fix this program? -


i'm getting error in few lines , don't know how program work. beginer programmer here. appreciated.

#include <iostream> #include <cstring>  using namespace std;  char* find_greater(char* a, char b) {     char* result;     if (strcmp(a, b) > 0)          result = a;       else         result = b;     return result; }  int main() {     char str1[10], str2[10];     str1[] = "zebra";     str2[] = "man";     cout << "greater string :" << find_greater(str1, str2) << endl;     return 0; } 

two problems.

  1. a silly typo:

    char* find_greater(char* a, char* b) //                              ^ 

    pay more attention you're doing, , read code.

  2. parse error here:

    char str1[10], str2[10]; str1[] = "zebra"; str2[] = "man"; 

    you can't assign string literals arrays and, if could, str1[] not thing. special case can initialise (only) that, so:

    char str1[10] = "zebra", str2[10] = "man"; 

fixing those, the code compiles , runs (though, explored in other answers, not best approach).


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -