0

I'm still a beginner in C, but as training I would like to create a Password Manager(a very bad on though). First a .txt file is created, and then the user is given an option of what they would like to do next. Using strcmp I make the program decide what function to execute next. I have two, storeServiceEmailPass(), where the user saves info to the .txt document and readAllData(), where the program prints out all text saved on the document. Using if, else if and strcmp, I make the program choose a function based of the user input.

When I execute the function getUserInput(), it gives me the following warning:

comparison of array 'userInput' equal to a null pointer is always false [-Wtautological-pointer-compare] 

and then stops the program, stating: Segmentation fault. It is impossible that this is a null pointer like the error suggests.

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

void createPassDoc() {
    FILE * passfile = fopen("Passwords.txt", "w");
    fclose(passfile);
    printf("File Created.\n");
}

void storeServiceEmailPass() {
    char ServiceEmailPassword[300];
    FILE * passfile = fopen("Passwords.txt", "a");
    printf("Please enter the following: Service, Email, Password.\n");
    fgets(ServiceEmailPassword, 300, stdin);
    fprintf(passfile, "%s", ServiceEmailPassword);
    fclose(passfile); 
}

void readAllData() {
    char savedData[5000];
    FILE * passfile = fopen("Passwords.txt", "a");
    printf("Here is your saved Data:\n");
    while(fgets(savedData, 100, passfile)) {
        printf("%s\n", savedData);
    }
    fclose(passfile);
}

void getUserInput() {
    char userInput[255];
    char createPass[] = "Store Service\n";
    char readAllPass[] = "Show Passwords\n";

    printf("What would you like to do?\n");
    fgets(userInput, 255, stdin);
    if(strcmp(createPass, userInput == 0)){
        storeServiceEmailPass();
    } else if (strcmp(readAllPass, userInput == 0)){
        readAllData();
    } else {
        printf("Please enter a command such as: Store Service or Show Passwords.\n");
    }
}

int main() {

    createPassDoc();
    getUserInput();

    return 0;
}

I expected function getUserInput to execute, but it just tells me

segmentation fault 

and also gives me the warning:

comparison of array 'userInput' equal to a null pointer is always false [-Wtautological-pointer-compare] 

for lines 38 & 40

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    There is some fundamental misunderstanding. "When I execute the function..." No. That warning is a compiler warning. You get it when you compile your code. Not when you execute that function or run your code. You should not even run your code if you get compiler warnings or errors you do not understand. – Gerhardh Jan 03 '23 at 13:31
  • As a beginner, compile with `gcc -std=c17 -pedantic-errors -Wall -Wextra -Werror`. Then you don't have to waste time chasing down bugs like this that the compiler has already found. – Lundin Jan 03 '23 at 13:32
  • Don't you love C? A string compare with a boolean? It's the Chernobyl of programming: "But they ignored the warning light!" – Peter - Reinstate Monica Jan 03 '23 at 13:35
  • @Peter-ReinstateMonica The problem isn't C but lax compilers generating a binary executable even when encountering constraint violations. _"The programmer probably wanted the program to crash, who am I to deny it?"_ - gcc. – Lundin Jan 03 '23 at 13:40
  • @Lundin Where, in your opinion, is the constraint violation? I think the code is perfectly valid C . – Peter - Reinstate Monica Jan 03 '23 at 14:31
  • @Lundin And btw: The warning only refers to the test `userInput == 0` (which is perfectly legal C but always false), not to the conversion of the resulting 1 to a char pointer. – Peter - Reinstate Monica Jan 03 '23 at 14:35
  • 1
    @Peter-ReinstateMonica The result of the `==` is of type `int`. Arguments are passed to functions "as per assignment". Meaning it's a constraint violation of simple assignment, since the left operator of = is a (qualified) pointer and the right one is an arithmetic type, which is not one of the valid forms of simple assignment. See 6.5.16.1 - constraints. Thus the compiler must generate a diagnostic message or it is non-conforming. Details here: https://stackoverflow.com/questions/52186834/pointer-from-integer-integer-from-pointer-without-a-cast-issues – Lundin Jan 03 '23 at 14:39
  • Also even gcc running in the default "absent-minded gnu mode" is still giving a warning "warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast", so it is conforming even then. In fact I just now realized that the OP is wrong, they aren't using gcc but clang. Which also gives a correct diagnostic... I retagged the question. – Lundin Jan 03 '23 at 14:44
  • @Lundin You are right... seems like no standard ever permitted int -> pointer conversions without a cast! And Clang 15 actually refuses to compile that. – Peter - Reinstate Monica Jan 03 '23 at 15:32

1 Answers1

2
if(strcmp(createPass, userInput == 0)){

You just have your parentheses misplaced. The comparison should be against the result of the function call, not the second argument.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157