-3

How to fix the warning?

warning: assignment to ‘struct people *’ from incompatible pointer type ‘char *’

format ‘%s’ expects argument of type ‘char ’, but argument 3 has type ‘char ()[100]

/* loading user information from the file*/
    void load_file(struct people *head)
    {
    FILE *pFile;
    char line[N];
    char temp_name[100];
    char temp_username[100];
    char temp_birthPlace[100];
    char new_people[100];
    char temp_the_date_of_birth[100];
    //open the FILE
    if (pFile = fopen("facebook", "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);
            strcpy(new_people->name, temp_name) == 0;
            strcpy(new_people->username, temp_username) == 0;
            strcpy(new_people->the_date_of_birth, temp_the_date_of_birth) == 0;
            strcpy( new_people->birthPlace, temp_birthPlace) == 0;
                // adding new people and then putting them as head at the beginning of the linked list
            new_people->next = head;
            head = new_people;
        }
        fclose(pFile);
        printf("file exists");
    }
    else
    {
        printf("file does not exist");
    }
    return;
}

//sort out users and list up their personal information along with the date of birth and birthplace
void invite_group(struct people *head)
{
    FILE *pFile;
    char temp_username[100];
    char temp_birthplace[100];
    char temp_the_date_of_birth[100];
    struct people *ptr;
    struct people *temp_head = malloc(sizeof(temp_head));
    *temp_head = *ptr;

    This ArrayList also allows users to add and sort items in the list.
    struct people *ListArray[100];
    int count = 0;
    printf("please input username who invites you\n");
    scanf("%s", ptr->username);
     while ((ptr->next) != NULL)
     {
        if(temp_username == ptr->username)
          {
            pFile = fopen("test.txt", "r");
          }
            if (pFile != NULL)
            {
                //input username, the date of birth and birthplace into the file
                fprintf(pFile, "username %s,the_date_of_birth %s,birthPlace %s", ptr->username, ptr->the_date_of_birth, ptr->birthPlace);
                // list and display username, the date of birth and birthplace
                ListArray[count++] = ("%s", "%s", "%s", ptr->username,ptr->the_date_of_birth, ptr->birthPlace);
            }
            ptr = ptr->next;
    }
    return;
  }

James Z
  • 12,209
  • 10
  • 24
  • 44
Taja
  • 9
  • 4
  • 1
    It probably means you're wrongly using `&` when calling `scanf` with `%s` and a char array which you want to read the string into. `scanf` is weird in all sorts of ways, one of which is that you always need to use `&`, *except* with `%s`. – Steve Summit Dec 08 '21 at 21:55
  • 1
    The same applies for compiler errors. They are text. You can just add it as text directly into your question. – Gerhardh Dec 08 '21 at 22:12
  • 1
    You may want to read this: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/12149471) – Andreas Wenzel Dec 08 '21 at 23:38
  • @SteveSummit ... and _except_ with `%[...]`. – chux - Reinstate Monica Dec 09 '21 at 02:11
  • [Images](//meta.stackoverflow.com/q/285551/90527) should not be used for textual data, such as code and error messages. – outis Apr 15 '22 at 04:22

1 Answers1

1

The conversion specifier %s expects an argument of the type char *.

For example if you have a character array like this

char temp_name[100];

then used in expressions it is converted to pointer of the type char * to its first element.

So you may write

sscanf(line, "%s", temp_name );

On the other hand, the expression &temp_name has the type char ( * )[100]. That is it is a pointer to the array as a whole object. So this expression may not be used in the above call of sscanf because it does not have the type char *..

Also pay attention to that the function is in any case incorrect.

void load_file(struct people *head)

It deals with a copy of the value of the passed to it pointer. Changing the copy within the function does not influence on the value of the original pointer used as a function argument. So after executing the function the original pointer stays unchanged. You need to pass it to the function by reference. That is the function should be declared like

void load_file(struct people **head);

And within the function you have to write

        new_people->next = *head;
        *head = new_people;

And if in the function caller you have pointer

struct people *head;

then the function is called like

load_file( &head );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • this is very informative. The warning has just been cleared. I'm also wondering if you could help me with this warning? 184 | ListArray[count++] = ("%s", "%s", "%s", ptr->username,ptr->the_date_of_birth, ptr->birthPlace); – Taja Dec 08 '21 at 22:11
  • @Taja The statement with the warning is just incorrect.. In the right side of the assignment there is used an expression with the comma operator. Its value is the value of the last operand. But the object that is assigned to does not have the type char *. – Vlad from Moscow Dec 08 '21 at 22:16
  • Thank you for your comment! hmm, can you please elaborate or give me some examples to let me understand how to fix the warning? – Taja Dec 08 '21 at 22:46
  • @Taja: The [comma operator](https://en.cppreference.com/w/c/language/operator_other#Comma_operator) does something very different than what you want to accomplish. My guess is that you need to write something like `sprintf( ListArray[count++], "%s %s %s", ptr->username,ptr->the_date_of_birth, ptr->birthPlace);` instead. – Andreas Wenzel Dec 08 '21 at 23:58