【问题标题】:Expected expression and undeclared identifier error预期的表达式和未声明的标识符错误
【发布时间】:2021-11-27 04:06:45
【问题描述】:

当我在一行中初始化一个指向结构变量的指针时,我得到error: expected expression,但是如果我在没有声明的情况下定义了变量,它就可以工作。我想在一行中定义所有结构指针,而不先进行前向声明,因为如果没有,那么我的文件会变得很长。

关于如何解决这些问题有什么建议吗?还是没有办法规避这个问题?

...
//a Node class is defined before, similar to a Node in a linked list

struct Node *buildast(struct Node *t) {
  int tag = node_get_tag(t);

  switch (tag) {
      //i dont want to keep declaring my variables likes below
      struct Node *root;
      struct Node *everything_else;
    
    case NODE_translation_unit:
      //To fix my issue, I declared my variables before
      root = node_alloc_str_copy(AST_ROOT, node_get_str(t));
      everything_else = node_get_kid(t, 0);
      node_add_kid(root, everything_else);
      return buildast(everything_else);

    case NODE_definition: 
        //i want to define my struct pointert to a Node in
        //one line like below but get an error
        //when I try to use the below variable later, I get an 
        //undeclared identifier to 'child'
       struct Node *child = node_get_kid(t, 0); //this line gives an error
       if (node_get_num_kids(child) <= 1) {
        node_add_kid(t, child);
        return buildast(child);
       }
       
       ...

【问题讨论】:

    标签: c pointers struct


    【解决方案1】:

    这是个问题:

    case NODE_definition: 
       struct Node *child = node_get_kid(t, 0);
    

    因为您将标签应用于声明。标签只能应用于语句

    无需重组的简单方法是在case 标签之后添加一个空语句:

    case NODE_definition: 
       ;
       struct Node *child = node_get_kid(t, 0);
    

    或者使用复合语句:

    case NODE_definition: 
    {
       struct Node *child = node_get_kid(t, 0);
        ...
    }
    

    不过,一般来说,通过在switch 之前放置任何声明之前,您的代码会更简洁,因此您无需在其中定义任何内容并担心这些类型的问题。

    【讨论】:

      【解决方案2】:

      假设这...

      当我在一行中初始化一个指向结构变量的指针时,我得到error: expected expression,但如果我在没有声明的情况下定义了变量。

      ... 旨在描述为此 ...

      标记的错误
          case NODE_definition: 
              //i want to define my struct pointert to a Node in
              //one line like below but get an error
              //when I try to use the below variable later, I get an 
              //undeclared identifier to 'child'
             struct Node *child = node_get_kid(t, 0); //this line gives an error
      

      ...您误解了问题的本质。如果确实为此发出的错误消息是“预期表达式”,那么您应该切换到更好的编译器,因为该诊断极具误导性。

      对比 GCC 对同一行的诊断:

      t.c:11:13: error: a label can only be part of a statement and a declaration is not a statement
                   struct node *child = node_get_kid(t, 0);
      

      如果您依赖于变量child 的先前声明,那么剩下的...

                   child = node_get_kid(t, 0);
      

      ... 是一个语句,因此可以被标记。虽然它实际上是一个表达式语句,由一个表达式组成,但所有其他类型的语句也可以在其中使用——例如break; 语句。表达式不是必须的。

      至于稍后使用变量,变量声明的范围是包含它的最里面的块(如果有的话),在这种情况下是switch 主体。如果你想在switch 之外使用变量,那么你必须在switch 之前声明它(而不是在case 标签之前):

        struct Node *child;
      
        switch (tag) {
      
          // ...
      
          case NODE_definition: 
              child = node_get_kid(t, 0);
      
          // ...
        }
      
        // you can still use 'child' here
      

      如果你只想要一个case,那么你可以将它插入到一个块中(a.k.a.一个复合语句):

        switch (tag) {
      
          // ...
      
          case NODE_definition: {
              struct Node *child = node_get_kid(t, 0);
      
              // ...
      
              // the above declaration of 'child' is in scope (only) up to this point
          } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-12
        • 1970-01-01
        • 2013-01-22
        • 2011-10-25
        • 2011-02-15
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        相关资源
        最近更新 更多