1

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
  • 3
    Why is Token a char** instead of e.g. a char* (i.e. why the double pointer) ? Commented Sep 4, 2024 at 15:33
  • 4
    There are many things that should be fixed, but perhaps you just want else if (Token[i][0] == '=') Commented Sep 4, 2024 at 15:36
  • 2
    You need to provide minimal code that can compile in order to replicate the issue. When attempting to utilize the included code, the program does not compile. Commented Sep 4, 2024 at 15:37
  • @Bho To compare two strings use standard function strcmp as for example } else if( strcmp(Token[i], "=" ) == 0 ){ Commented Sep 4, 2024 at 15:45
  • 1
    @VladfromMoscow, i tried now and work, tanks. Commented Sep 4, 2024 at 15:48

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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.