i’m not sure what’s going on. New to C and trying to make a simple program that just checks if two strings are equal. (It’s supposed to be a simple Hash Validator)

Here’s the code:

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

main()
{
    char hash1, hash2;
    char result;
    // read hashes as strings
    printf("Insert first hash\n");
    scanf("%s", &hash1);
    printf("Insert second hash\n");
    scanf("%s", &hash2);

    result = strcmp(hash1, hash2);
    
    if (result == "0") {
        printf("VALID MATCH");

    }   else {
        printf("INVALID MATCH");
    }
    return 0;
}

Here are the two error messages I get when the code stops at the strcmp line:
“message”: “passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]”
“message”: “passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]”

Thanks in advance

Didn’t know what to try honestly. I tried doing some research but users have an issue with mixing up integers and stirngs resulting in the same error. I’m not sure what’s going on here.

New contributor

shuichisaito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

5

hash1 and hash2 are single char variables, which get promoted to int when passed to functions. You should defined these as char arrays.

Also correct some other issues:

  • main() is incorrect: main without arguments should have a prototype int main(void)
  • pass scanf the maximum number of characters to store into the destination char arrays not counting the null terminator.
  • test the return value of scanf().
  • result should have type int and if it is non zero, only the sign of its value matters.
  • if (result == "0") is incorrect: you should write if (result == 0)

Here is a modified version:

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

int main(void) {
    char hash1[100], hash2[100];
    int result;

    // read hashes as strings
    printf("Insert first hash\n");
    if (scanf("%99s", hash1) != 1) {
        printf("invalid or missing inputn");
        return 1;
    }
    printf("Insert second hash\n");
    if (scanf("%99s", hash2) != 1) {
        printf("invalid or missing inputn");
        return 1;
    }

    result = strcmp(hash1, hash2);

    if (result == 0") {
        printf("VALID MATCHn");
    } else {
        printf("INVALID MATCHn");
    }
    return 0;
}

Khám phá các thẻ bài đăng