错误:预期';',标识符或'''在'struct'之前

时间:2012-12-21 00:17:31

标签: c struct

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开始的行中遇到以下错误:

  

期望';',标识符或'('''''''''''''。

3 个答案:

答案 0 :(得分:3)

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

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

应替换为:

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

答案 1 :(得分:0)

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

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

struct some_struct
{

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

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

};

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

答案 2 :(得分:0)

在分号之前和结束花括号之后添加结构别名,允许我编译结构(在添加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;