How to compare an array with a char using if in C?

  Kiến thức lập trình

I ask for your help. I need to compare an array of tokens with a char using the if statement. But in the array I also have a string, the code is this:

char bho(char** Token){
    char* token = (char*)Token;
    enum TokenType type;

    int len_token = strlen(token);
    
    for (int i = 0; i < len_token; i++){
        if(Token[i] == NULL){
            continue;
        }else if (Token[i] == '='){
            type = Equals;
            printf("{value: %s, type: %d}n", Token[i], type);   
        } else {
            printf("{value: %s}n", Token[i]);
        }
    }

}

int main(void){
    char input[] = "Ciao = l";
    char** array;

    array = tokenizer(input);
    
    bho(array);

}

tokenizer() is a function that splits the input string and put the parts into an array. The enum TokenType is an enum that I need to categorize the type of all the content of the array.

The problem is that my program has the output of:

output:
{value: Ciao}
{value: =}
{value: l}

but the output it has to give me is:

output:
{value: Ciao}
{value: =, type: 4}
{value: l}

I do:

for (int i = 0; i < len_token; i++){
    if(Token[i] == NULL){
        continue;
    } else if(Token[i] == '='){
        type = Equals;
        printf("{value: %s, type: %d}n", Token[i], type);
    } else {
      printf("{value: %s}n", Token[i]);
    }
}

but give me the error: comparison between pointer and integer.
I tried various methods but can’t make it work.

Sorry if I wrote with incorrect grammar but i did fatigue with the write in English.

6

There were several ways that your posted source code used types that needed a few changes.

Since your post did not include the entire code such as the source for the function tokenizer(), I made an assumption or two.

Here is a solution with those assumptions and works with the test data I put together.

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

// added type since it's not in the post.
enum { Unknown = 0, Equals = 4 } TokenType;

char bho(char** Token) {
    enum TokenType type = Unknown;

    // go through the array of strings checking each
    // string to determine the token's type.
    // we assume the last entry is a guard value of NULL
    // indicate the end of the array.
    for (; *Token != NULL; Token++) {
        if (strcmp (*Token, "=") == 0) {
            type = Equals;
            printf("{value: %s, type: %d}n", *Token, type);
        }
        else {
            printf("{value: %s}n", *Token);
        }
    }

    return 0;
}

int main(void) {
    char input[] = "Ciao = l";

    // create sample test data This assumes that the function
    // tokenizer creates an array of pointers to strings and
    // the last item in the array is a NULL pointer indicating
    // no more data.
    char* array[] = { "Ciao", "=", "1", NULL };

 // comment out this unknown function in order to use
 // the test data.
 //   array = tokenizer(input);

    bho(array);

    return 0;
}

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT