【问题标题】:What do I do to stop an error "expected expression before ‘char’"?我该怎么做才能停止错误“'char'之前的预期表达式”?
【发布时间】:2013-02-27 12:45:45
【问题描述】:

我正在编译我的工作,无论我如何编辑我的代码,这个错误都会不断出现:

expected expression before ‘char’

format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

从第二个错误开始,我尝试使用类型转换,但问题仍然存在。有谁知道怎么做? 这是我的代码的一部分:

while ( char my_wget (char web_address[BUFLEN]) != EOF ) {
    printf ("%s", (char) web_address[BUFLEN]);

【问题讨论】:

  • 买一本关于 C 的书并从中学习语言。不要只是希望它会编译,更别说工作了。

标签: c compiler-construction casting char format


【解决方案1】:

printf() 语句中,尝试将char 部分更改为char* 同样适用于while 循环中的条件。将web_address之前的char改成(char*)

我觉得你在my_wget() 之前写"char" 很奇怪。能具体点吗?

【讨论】:

    【解决方案2】:

    因为您在编写 charchar 时遇到了语法错误。

    也许你已经想到了:

    int ch;
    char web_address[BUFLEN];
    
    while ((ch = my_wget(web_address)) != EOF)
        printf("%s\n", web_address);
    

    使用正确的my_wget() 声明(例如extern int my_wget(char *buffer);),应该可以编译。你不能到处声明变量。

    第二个错误是因为web_address[BUFLEN] 是一个字符(当然在我的代码中;它似乎也在你的代码中,因为编译器设法充分识别类型以生成错误)。如果您像我一样声明它,它也是超出数组末尾的一个。将char 值(可能是 8 位数量)视为地址(指针;可能是 32 位或 64 位数量)是错误的,这就是编译器抱怨的原因。

    【讨论】:

      【解决方案3】:

      请看下面这段代码

      #include <stdio.h>
      
      int main()
      {
      char c;
      printf ("%s",  st);
      }
      

      当我编译它时,我会收到同样的警告信息。

       warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’
      

      所以我把程序改成

      #include <stdio.h>
      
      int main()
      {
      char *str = "string";
      printf ("%s",  st);
      }
      

      现在程序可以正常编译了。

      所以作为一个 c 的新手,这就是你学习语言的方式,写最小的例子,以证明你对这个概念有牢牢的把握。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-30
        • 2021-06-06
        • 1970-01-01
        • 1970-01-01
        • 2013-10-03
        • 1970-01-01
        • 2014-02-22
        • 2011-12-05
        相关资源
        最近更新 更多