0
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开始时出现以下错误:

预期 ';'、标识符或 '(' 在 'struct' 之前"。

4

3 回答 3

3

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

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

应替换为:

 struct proc_time proc; /* process with given pid or current process */ 
于 2012-12-21T00:19:44.480 回答
0

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

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

struct some_struct
{

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

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

};

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

于 2012-12-21T00:28:50.247 回答
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;
于 2015-11-18T06:32:02.250 回答