C错误:格式'%s'期望类型为'char *'的参数,但参数2的类型为'char(*)[100]' [英] C error:format '%s' expects argument of type 'char *'but argument 2 has type 'char (*)[100]'

查看:60
本文介绍了C错误:格式'%s'期望类型为'char *'的参数,但参数2的类型为'char(*)[100]'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天我正在练习运动,并且收到此警告(如标题所示)。我尝试了很多东西,但我真的不知道该如何解决。我不太擅长编程,所以会出错。以下是我正在使用的结构(由于给出的方式而无法更改):

I'm working on an exercise in c the last few days and I'm having this warning (as the title suggests). I've tried a bunch of stuff but I don't really know how to exactly fix this. I'm not good at programming so there are mistakes. Below are the structs I'm using (which cannot be changed because that's how they are given):

    typedef struct bookR* book;
struct bookR{
    char author[MAXSTRING];
    enum genres{fiction,scientific,politics};
    int id;
    char review[MAXLINES][MAXSTRING];

};

typedef struct nodeR* node;
struct nodeR{
    book b;
    node next;

};

typedef struct listR* list;
struct listR{
    node head, tail; 
    int size;
};

这是发生问题的部分代码:

And here is part of the code where the problem occurs:

 void addBook(book b, list bList){
char author [MAXSTRING];
int id;
char review [MAXSTRING][MAXLINES];
printf ("Give the author,`enter code here` id and review of the new book respectively");
scanf("%s",author);
scanf("%d",&id);
scanf("%s",review);
node k=(node)malloc(sizeof(struct nodeR));
assert(k);
k->next=NULL;
strcpy(k->b->author,author);
k->b->id=id;
strcpy(k->b->review,review[MAXSTRING]);}

这是我得到的警告:

  warning: format '%s' expects argument of type 'char *' but argument 2 has type 'char (*)[100]' [-Wformat=]
scanf("%s",review);
warining:passing argument 1 of 'strcpy' from incompatible pointer tupe [-Wincompatible-pointer-types]
strcpy(k->b->review,review[MAXSTRING]);

任何帮助都非常感谢。

Any help is much appreciated. Thanks for your time and sorry for the long post.

推荐答案


  • 警告No1

    要使用 scanf ,您需要传递指针。您已声明:

    In order to use scanf, you need to pass a pointer to it. You have declared :

    char review [MAXSTRING][MAXLINES];
    

    但是您读到了:

    scanf("%s",review);
    

    您需要将其更改为:

    scanf("%s", review[i]);
    

    其中 i 是<$的索引c $ c> 0 到 MAXSTRING-1

    警告2

    此外,该语句:

    strcpy(k->b->review,review[MAXSTRING]);
    

    越界越好,因为数组的位置达到 review [ MAXSTRING-1] 。除此之外,您还要为整个 array 分配 line 。因此,您应该将其更改为:

    gets out of bounds, as you the positions of your array reach review[MAXSTRING-1]. Apart from that, you are assigning a line to a whole array. So you should change it to :

    strcpy(k->b->review[index], review[MAXSTRING-1]);
    


  • 再加上两个注释:


    1. 请参阅此有关为什么不强制转换malloc结果的链接

    2. 继续请注意,在这样的声明中:

    1. See this link on why not to cast the result of malloc.
    2. Keep in mind that in a declaration such as :

    array[x][y];
    

    x 表示行, y 指示列。您以相反的方式使用它们,因此请确保不要混淆,并且在需要列时真正访问行。在列时需要行。

    x indicates the lines and y indicates the columns. You have them in the opposite way, so make sure that you do not get confused and that you really access rows when you want rows and columns when you want columns.

    这篇关于C错误:格式'%s'期望类型为'char *'的参数,但参数2的类型为'char(*)[100]'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆