简体   繁体   English

在 C 中,如何修复此警告:格式“%s”需要“char *”类型的参数,但参数 3 的类型为“char (*)[100]

[英]In C, how to fix this warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[100]

Please tell me how to fix the warning.请告诉我如何修复警告。 Please kindly check the picture attached here and help me with the given warning.请检查附在此处的图片并帮助我解决给定的警告。 I really appreciate your time and help...我真的很感谢你的时间和帮助......

warning: assignment to 'struct people *' from incompatible pointer type 'char *'警告:从不兼容的指针类型'char *'分配给'struct people *'

format '%s' expects argument of type 'char ', but argument 3 has type 'char ( )[100]格式“%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;
  }

The conversion specifier %s expects an argument of the type char * .转换说明符%s需要一个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.然后在表达式中使用它被转换为char *类型的指针指向它的第一个元素。

So you may write所以你可以写

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

On the other hand, the expression &temp_name has the type char ( * )[100] .另一方面,表达式&temp_name的类型为char ( * )[100] That is it is a pointer to the array as a whole object.也就是说,它是指向整个数组 object 的指针。 So this expression may not be used in the above call of sscanf because it does not have the type char * ..所以这个表达式可能不会在上面的sscanf调用中使用,因为它没有类型char * ..

Also pay attention to that the function is in any case incorrect.还要注意 function 在任何情况下都是不正确的。

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.更改 function 中的副本不会影响用作 function 参数的原始指针的值。 So after executing the function the original pointer stays unchanged.所以在执行 function 之后,原始指针保持不变。 You need to pass it to the function by reference.您需要通过引用将其传递给 function。 That is the function should be declared like那就是 function 应该声明为

void load_file(struct people **head);

And within the function you have to write在 function 中你必须写

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

And if in the function caller you have pointer如果在 function 调用者中有指针

struct people *head;

then the function is called like然后 function 被称为

load_file( &head );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:格式“%s”需要类型“char *”,但参数 2 的类型为“char (*)” - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)’ C错误:格式'%s'需要'char *'类型的参数,但参数2的类型为'char(*)[100]' - C error:format '%s' expects argument of type 'char *'but argument 2 has type 'char (*)[100]' 警告:格式%s期望为char *类型,但参数2为int类型 - warning: format %s expects type char * but argument 2 has type int 警告:格式'%s'需要类型'char *',但参数2的类型为'int' - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’ 警告:格式'%c'需要类型'int',但参数2的类型为'char *' - warning: format '%c' expects type 'int', but argument 2 has type 'char *' 格式为'%s',参数类型为'char *',但是参数2的类型为'char **' - format '%s' expects argument of type 'char *', but argument 2 has type 'char **' 格式 '%c' 需要类型为 'char *' 的参数,但参数 2 的类型为 'char (*)[0]' - format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[0]’ 警告:格式'%s'期望的参数类型为'char *',但是参数2的类型类型为'char(*)[30]'[-Wformat =] - warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[30]’ [-Wformat=] 警告:格式'%s'期望的参数类型为'char *',但是参数2的类型为'char' - warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char' 格式为'%s',参数类型为'char *',但是参数2为类型'int' - format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM