1

I am trying to write C code to accept characters from a user one by one and then store them into a character array. If the user enters "1" I want the loop to terminate. The problem is that when I run the program, it just terminates and I get an error saying:

incompatible integer to pointer conversion passing 'char'
      to parameter of type 'const char *'; take the address with &
      [-Wint-conversion]
    while (strcmp(ch, "1") != 0)

How do I correct this? BTW, I'm using the WinGW-w64 compiler for Windows 10.

I have this code thus far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100

int main(void)
{
    int i = 0;
    char ch, string[MAX];

    while (strcmp(ch, "1") != 0)
    {
        printf("Enter a character: ");
        scanf("%c", &ch);
        fflush(stdin);

        string[i] = ch;
        i++;
    }

    printf("%c", string[0]);
}
3
  • 6
    while (strcmp(ch, "1") != 0) -> while (ch != '1') Commented Apr 21, 2021 at 7:45
  • Please don't tag c++ in c questions, they're separate languages Commented Apr 21, 2021 at 7:46
  • 1
    As you have already been answered, strcmp is a function which compares 2 strings. The function expects to receive two const char *, but one of your arguments is a char. Commented Apr 21, 2021 at 8:01

2 Answers 2

3

You have one character. strcmp requires a pointer to a sequence of characters that end with 0.

The easiest thing is to compare your one character to one character

while (ch != '1')
Sign up to request clarification or add additional context in comments.

1 Comment

Another issue remains with char ch; ... while (ch != '1'), the initial value of ch is indeterminant.
-1

Try this.

Solution.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
    
    int main(void)
    {
        int i = 0, j = 0;
    
        char ch[MAX] = {0}, finalResult[MAX] = {0};
    
        //type cast with the strcmp argument
        //as per the function defination @strcmp will take "const char *", argument type
        while (strcmp((const char*)&ch, "1") != 0)
        {
            printf("Enter a character: ");
    
            scanf("%c", ch);   //as you are reading one 1 character so
    
            fflush(stdin);
    
            finalResult[j++] = ch[0];  //update user input character to the destination buffer
    
            i++; //increment for the putting '\0' to compare with "1"
    
            ch[i] = '\0';  //add null
    
        }
    
        printf("%c", finalResult[0]);
    }

3 Comments

Strictly speaking (const char*)&ch is wrong. If you take &ch where ch is an array, you get a pointer to the array of type char (*)[MAX]. This pointer is not compatible with const char*. So just drop the &ch and use ch.
Let me clear why i took (const char *)&ch, first char ch[MAX] is local variable and function need a pointer argument so if you want to pass the local variable to any pointer function how you will pass it ? does typecast is necessary ? if not passing the location variable without casting does it work fine ? or garbage will come ?
Arrays decay into a pointer to the first element whenever used in a normal expression like strcmp(ch, "1"). Pretty much the only exception to that array decay rule is when you use & on an array, which there is almost never a reason to do. Study arrays and pointers in your favourite C programming book.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.