【问题标题】:error: expected ‘;’, identifier or ‘(’ before ‘struct’错误:预期为“;”、标识符或“结构”之前的“(”
【发布时间】:2012-12-21 00:17:31
【问题描述】:
struct proc_time /* info and times about a single process*/ 
{ 
    pid_t pid; /* pid of the process*/ 
    char name[16]; /* file name of the program executed*/ 
    unsigned long start_time; /* start time of the process*/ 
    unsigned long real_time; /* real time of the process execution*/ 
    unsigned long user_time; /* user time of the process*/ 
    unsigned long sys_time; /* system time of the process*/ 
}; 

struct proctimes /* info and times about all processes we need*/ 
{ 
    struct proc_time proc; /* process with given pid or current process */ 
    struct proc_time parent_proc; /* parent process*/ 
    struct proc_time oldest_child_proc; /* oldest child process*/ 
    struct proc_time oldest_sibling_proc; /* oldest sibling process*/ 
};

我无法理解我的声明出了什么问题,并且在第二个 struct 开始的行中出现以下错误:

“结构”之前应为“;”、标识符或“(”。

【问题讨论】:

  • 你能把之前的代码贴出来吗?

标签: c struct


【解决方案1】:

问题是您使用 /.../ 作为非法的注释分隔符。行:

struct proc_time proc; /process with given pid or current process/ 

应替换为:

 struct proc_time proc; /* process with given pid or current process */ 

【讨论】:

  • 是的,它甚至在语法高亮中可见。
  • struct proc_time proc; // process with given pid or current process
  • 感谢您的回答。我的代码中有 /* */。只是在输入问题时忘记了它。cmets 没有问题。
【解决方案2】:

如果您在 文件范围 中声明这些结构体(假设您修复了注释问题),这并没有发生这种情况的真正原因。

但是,如果你设法在一个更大的结构中声明这些结构,那么你确实会从 C 编译器中得到一个错误

struct some_struct
{

  struct proc_time
  { 
    ...
  }; /* <- error: identifier expected */

  struct proctimes
  { 
    ...
  }; /* <- error: identifier expected */

};

在 C 语言中,以“嵌套”方式声明结构类型而不立即声明该类型的数据字段是非法的。

【讨论】:

    【解决方案3】:

    在分号之前和右花括号之后添加结构别名,允许我编译你的结构(在添加 main 方法并包括 stdlib.h 之后使用 gcc)。

    struct proc_time /* info and times about a single process*/ 
    { 
        pid_t pid; /* pid of the process*/ 
        char name[16]; /* file name of the program executed*/ 
        unsigned long start_time; /* start time of the process*/ 
        unsigned long real_time; /* real time of the process execution*/ 
        unsigned long user_time; /* user time of the process*/ 
        unsigned long sys_time; /* system time of the process*/ 
    } proc_time; 
    
    struct proctimes /* info and times about all processes we need*/ 
    { 
        struct proc_time proc; /* process with given pid or current process */ 
        struct proc_time parent_proc; /* parent process*/ 
        struct proc_time oldest_child_proc; /* oldest child process*/ 
        struct proc_time oldest_sibling_proc; /* oldest sibling process*/ 
    } proctimes;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      相关资源
      最近更新 更多