-1

Please tell me how to fix the warning. Please kindly check the picture attached here and help me with the given warning.

here's a new warning

struct people structures

    /* loading user information from the file*/
    void load_file(struct people *head)
    {
    FILE *pFile;
    char line[N];
    char temp_name;
    char temp_username;
    char temp_birthPlace;
    char new_people;
    char temp_the_date_of_birth;
    //open the FILE
    if (pFile == fopen("test1.txt", "r"))
    {
            // reading the contents of the file line by line
            while (fgets(line, N, pFile) != NULL)
        {
            struct people *new_people = (struct people *)malloc(sizeof(struct people));
            //Temporarily saving variables read from file
            sscanf(line, "%s,%s,%s,%s",&temp_name,&temp_username,&temp_the_date_of_birth,&temp_birthPlace);
            strcmp(new_people->name, temp_name) == 0;
            strcmp(new_people->username, temp_username) == 0;
            strcmp(new_people->the_date_of_birth, temp_the_date_of_birth) == 0;
            strcmp( new_people->birthPlace, temp_birthPlace) == 0;
                // adding new people and then putting them as head at the beginning of the linked list
            new_people = head;
            head = new_people;
            head->next = new_people;
        }
        fclose(pFile);
        printf("file exists");
    }
    else
    {
        printf("file does not exist");
    }
    return;
}
James Z
  • 12,209
  • 10
  • 24
  • 44
Taja
  • 9
  • 4
  • 1
    Does this answer your question? [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – Passerby Dec 04 '21 at 22:07
  • 1
    Do you mind showing the definition of `struct people`? Please edit the question including the warnings as text, not linked images, and a [mre]. – Bob__ Dec 04 '21 at 22:08

1 Answers1

0

The warning indicates you have not declared the types of the fields of your struct properly. You've declared them as int rather than char *.

struct people {
  char *username;
  char *birthPlace;
  int the_date_of_birth;
  struct people* next;
};

The next problem is you can't check string equality with == in C.

Strings in C are pointers to an array of characters terminated by a 0. That's all.

const char name[] = { 'T', 'a', 'j', 'a', 0 };
printf("Hello, my name is %s.\n", name);

If you do temp_head->username == temp_username you are checking if they point at the same memory. They clearly do not.

You need to compare the two arrays checking each character until you hit the 0 at the end. There's a function for that, strcmp. It's a little odd that it returns 0 for equality.

strcmp(temp_head->username, temp_username) == 0;
Schwern
  • 153,029
  • 25
  • 195
  • 336
  • It is not odd at all that `strcmp` returns 0 for equal strings. It's rather a pretty standard behavior of comparators they return a negative value if the first argument is less than the second one, positive if greater, and zero if they're equal. – CiaPan Dec 05 '21 at 10:03
  • @CiaPan When you first encounter that style of comparison, it's odd. – Schwern Dec 05 '21 at 20:10
  • It may seem odd if you just want to recognize some keyword like a command or an identifier. But when you think about binary searching in an array or a tree it turns out the standard behavior is far more useful. :) – CiaPan Dec 05 '21 at 22:15