#Error when comparing two chars

9 messages · Page 1 of 1 (latest)

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

void split() {
    char str[] = "The quick brown fox";
    for (int i = 0; i < strlen(str); i++) {
        if (strcmp(str[i], ' ') == 0) {
            printf("a");
        }
    }
}

int main() {
    split();

    return 0;
}```
viscid stratusBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

leaden tree
#
first.c: In function 'split':
first.c:7:23: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion]
    7 |         if (strcmp(str[i], ' ') == 0) {
      |                    ~~~^~~
      |                       |
      |                       char
In file included from first.c:2:
C:/msys64/mingw64/include/string.h:63:34: note: expected 'const char *' but argument is of type 'char'
   63 |   int __cdecl strcmp(const char *_Str1,const char *_Str2);
      |                      ~~~~~~~~~~~~^~~~~
first.c:7:28: warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion]
    7 |         if (strcmp(str[i], ' ') == 0) {
      |                            ^~~
      |                            |
      |                            int
C:/msys64/mingw64/include/string.h:63:52: note: expected 'const char *' but argument is of type 'int'
   63 |   int __cdecl strcmp(const char *_Str1,const char *_Str2);
      |                                        ~~~~~~~~~~~~^~~~~
first.c:7:13: warning: 'strcmp' reading 1 or more bytes from a region of size 0 [-Wstringop-overread]
    7 |         if (strcmp(str[i], ' ') == 0) {```
#

ig this isn't the correct way to do it

#

how can I compare if char at index i is equal to another char?

#
        if (str[i] == ' ') {
            printf("a");
        }```
#

did the job

woven raptor
#

yes. strcmp only works on strings (i.e. char *). == works on primitives, i.e. f.e. char.

viscid stratusBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.