【问题标题】:In C, how to fix this warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[100]在 C 中,如何修复此警告:格式“%s”需要“char *”类型的参数,但参数 3 的类型为“char (*)[100]
【发布时间】:2022-01-13 19:46:34
【问题描述】:

如何修复警告?

警告:从不兼容的指针类型“char *”分配给“struct people *”

格式“%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;
  }

【问题讨论】:

  • 这可能意味着您在调用 scanf%s 时错误地使用了 & 以及您想要将字符串读入的 char 数组。 scanf 在各方面都很奇怪,其中之一就是你总是需要使用&except%s
  • 编译器错误也是如此。它们是文本。您可以将其作为文本直接添加到您的问题中。
  • @SteveSummit ... 和 except%[...].

标签: c scanf c-strings implicit-conversion conversion-specifier


【解决方案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 );

【讨论】:

  • 这是非常有用的。警告刚刚被清除。我也想知道你是否可以帮助我解决这个警告? 184 | ListArray[count++] = ("%s", "%s", "%s", ptr->username,ptr->the_date_of_birth, ptr->birthPlace);
  • @Taja 带有警告的语句是不正确的。在赋值的右侧,使用了带有逗号运算符的表达式。它的值是最后一个操作数的值。但是分配给的对象不具有类型 char *.
  • 感谢您的评论!嗯,您能否详细说明或给我一些示例,让我了解如何修复警告?
  • @Taja:comma operator 所做的事情与您想要完成的事情完全不同。我的猜测是你需要写类似sprintf( ListArray[count++], "%s %s %s", ptr->username,ptr->the_date_of_birth, ptr->birthPlace); 的东西。
猜你喜欢
  • 2013-05-10
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多