-3

如何修复警告?

警告:从不兼容的指针类型'char *'分配给'struct people *'</p>

格式“%s”需要“char ”类型的参数,但参数 3 的类型为“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;
  }

4

1 回答 1

1

转换说明符%s需要一个类型为 的参数char *

例如,如果您有这样的字符数组

char temp_name[100];

然后在表达式中使用它被转换为指向char *其第一个元素的类型的指针。

所以你可以写

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

另一方面,表达式&temp_name的类型为char ( * )[100]。也就是说,它是指向整个对象的数组的指针。所以这个表达式可能不会在上面的调用中使用,sscanf因为它没有类型char *..

还要注意该函数在任何情况下都是不正确的。

void load_file(struct people *head)

它处理传递给它的指针值的副本。更改函数内的副本不会影响用作函数参数的原始指针的值。所以在执行函数后,原始指针保持不变。您需要通过引用将其传递给函数。那就是函数应该声明为

void load_file(struct people **head);

在你必须写的函数中

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

如果在函数调用者中有指针

struct people *head;

然后该函数被称为

load_file( &head );
于 2021-12-08T22:01:28.320 回答