C - [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]

c
25,026

Solution 1

Try this:

int main( void ) {
    char word[26], reverse[26];
    int length, i;

    printf("Enter word: ");
    scanf("%s", &word);   
    length=strlen(word);

    for (i=0; i<length; i++) {
        reverse[i] = word[length - i - 1];
    }

    /* Put \0 to terminate the string */
    reverse[length]='\0';
    printf("%s\n", reverse);
}

Note that in the for loop you have to use only < and not <= because you'll get out of bounds of the array (negative index) and you have to put the \0 terminator to terminate your reverse string. You don't need letter and btw strcpy works not with single chars, since a single char is not a string (because of the missing string terminator (\0)).

Edit:

int main( void ) {
    char word[26], reverse[26];
    int length, i;

    printf("Enter word: ");
    scanf("%s", &word);   
    length=strlen(word);

    for (i=0; i<length; i++) {
        reverse[i] = word[length - i - 1];

        /* Put \0 to terminate the string */
        reverse[i + 1]='\0';
        printf("%s\n", reverse);
    }       
}

Solution 2

Problems:

  • You pass the address of an array to scanf, rather than passing the array.
  • You don't copy the null termination correctly.
  • strcat must have null terminated strings as parameters, it cannot work with single characters. You don't need to use it here anyway.

Fixed program:

#include <stdio.h>
#include <string.h>

int main (void) 
{
  char word[26];
  char reverse[26]; 
  size_t length;
  size_t i;

  printf("Enter word: ");
  scanf("%s", word);

  length=strlen(word);
  for (i=0; i<length; i++) 
  {
    reverse[i] = word[length-i-1];
  }
  reverse[i] = '\0';

  printf("Reverse: %s\n", reverse);
  return 0;
}

Output:

Enter word: stackoverflow
Reverse: wolfrevokcats
Share:
25,026
Admin
Author by

Admin

Updated on July 18, 2022

Comments

  • Admin
    Admin almost 2 years

    There's a bunch of threads about C++, but I'm stuck with regular C here and can't find an answer anywhere.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    main() {
        char word[26], letter, reverse[26], length;
        printf("Enter word: ");
        scanf("%s", &word);
    
        length=strlen(word);
        for (int i=0; i<=length; i++) {
            letter = word[length-i];
            strcpy(reverse, letter);
            printf("%c\n", reverse);
        }
    
        getch();
    }
    

    I'm getting errors as the title states.

    13 25 ~ [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]

    51    18  C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h  [Note]
    

    initializing argument 2 of 'char* strcpy(char*, const char*)'

    What the code should do is reverse the written word and type it out line by line, by each last letter. Etc. - Type in "food" and the program should output:

    d

    od

    ood

    food

    I know I messed up somewhere in the 'letter', but I have no clue how to fix it. Also I'm trying to avoid using std::, personal prefrence.

    • kaylum
      kaylum over 7 years
      The message seems clear. letter is not a char * as required (ie, not a string). It is a single char.
    • user3386109
      user3386109 over 7 years
      You seem to be confused about the difference between a char and a string. In C, a string is an array of char. And the strcpy function expects two strings as arguments.
    • qxz
      qxz over 7 years
      More specifically, in C a string is a null-terminated array of chars.
    • qxz
      qxz over 7 years
      @Tremmert: You want to push letter onto the beginning of reverse?
    • David C. Rankin
      David C. Rankin over 7 years
      If it still isn't clear, when you declare char word[26], word is a pointer to the first character in a 26 character array. It is a pointer already. So when you need to provide a pointer to scanf, it is simply scanf("%s", word); (no & before word). Also always check the return of scanf to validate a successful conversion.
    • user3629249
      user3629249 over 7 years
      do not #include header files those contents are not used: I.E. conio.h
    • user3629249
      user3629249 over 7 years
      the function main() only has a very small number of valid return types (regardless of what Visual Studio allows) in the current scenario the signature for main() should be: int main( void )
    • user3629249
      user3629249 over 7 years
      When calling any of the scanf() family of functions, 1) always check the returned value, not the parameter value, to assure the operation was successful. 2) when using the %s format specifier, always include a MAX CHARACTERS modifier that is 1 less than the length of the input buffer so as to avoid any buffer overflow and the resulting undefined behavior, which can lead to a seg fault event
    • user3629249
      user3629249 over 7 years
      the posted code contains some 'magic' numbers. 'magic' numbers are numbers with no basis. I.E. 26. Strongly suggest using a #define or enum statement to give those magic numbers meaningful names, then use those meaningful names through out the code.
    • user3629249
      user3629249 over 7 years
      for ease of readability and understanding: follow the axiom: only one statement per line and (at most) one variable declaration per statement.
    • user3629249
      user3629249 over 7 years
      using the function: getch() is not portable. Suggest using: int ch; while( (ch = getchar()) != EOF && '\n' != ch ); getchar();
    • user3629249
      user3629249 over 7 years
      this line: strcpy(reverse, letter); is nonsense. The contents of letter is a single character and the function: strcpy() expects the second parameter to be a pointer.
    • Kami Kaze
      Kami Kaze over 7 years
      @user3629249 putting all this in an answer would have been easier and imho the better place (plus you get rewards for it)
    • user3629249
      user3629249 over 7 years
      @KamiKaze, other than the comment about strcpy(), none of the comments answer the OPs question. So they should not be used as an answer.
  • user3386109
    user3386109 over 7 years
    Sorry, but replacing one problem with another is not an answer.
  • kaylum
    kaylum over 7 years
    Giving a wrong answer and then saying why it is wrong is still not an answer. Just don't go there. Go straight to a fully correct answer.
  • David C. Rankin
    David C. Rankin over 7 years
    Read my comment to the question above.
  • Kami Kaze
    Kami Kaze over 7 years
    this is just plain wrong. reverese is an array so 'reverse' already is the address of the array, no need for '&'. And letter is still not a string just a pointer to a char.
  • Dhruv Khatri
    Dhruv Khatri over 7 years
    than why it will gives an error in code.? @KamiKaze
  • Dhruv Khatri
    Dhruv Khatri over 7 years
    Incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & this what my IDE suggested. @KamiKaze First make sure you are right after that give suggestion
  • alk
    alk over 7 years
    strcat() expects address to the 1st element of a C-"string", that is a 0-terminated array of char.
  • Dhruv Khatri
    Dhruv Khatri over 7 years
    So why my IDE suggest me to do like this @alk. And it will gives me the right answer also.
  • Kami Kaze
    Kami Kaze over 7 years
    you just have to modify the second argument. The first one is okay as it is. The clue is here 'initializing argument 2 of char* strcpy(char*, const char*). Don't know about your IDE, but I prefer reading the API and work with it than what some heuristic suggest might be right. As I said reverse already is an address, you could do &reverse[0] if it satisfies you. Insert snarky remark about your remark here....
  • Lundin
    Lundin over 7 years
    strcat must get null terminated strings as parameters, you cannot pass single chars. If this program "seems to work" it is because of sheer luck.
  • Lundin
    Lundin over 7 years
    (wolf revokes cats, wtf!?)
  • Lundin
    Lundin over 7 years
    Oops, didn't see this one and therefore posted a nearly identical answer. Have an up-vote.
  • alk
    alk over 7 years
    @KamiKaze: "The first one is okay as it is" just to avoid misunderstandings: char reverse[26]; ... strcat(reverse, ...): is okay. Doing strcat(&reverse, ... is not.
  • Kami Kaze
    Kami Kaze over 7 years
    @alk I should have been clearer, the first argument is good in the original code. Thx for clarifying this.
  • Admin
    Admin over 7 years
    It works well, but what I require is the 'reverse' being outputted multiple times, +1 letter at a time. So "food" should get output as d od ood food Thank you for the code, I'll work with it. Still getting all the quirks of C.
  • gmug
    gmug over 7 years
    In that case just move the printf line at the end of the for loop and don't forget to add the terminating \0 before you call the printf: reverse[i + 1]='\0';