首页 文章

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

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

expected';',identifier或'('before'struct'“ .

3 回答

  • 3

    问题是你使用/.../作为注释分隔符是非法的 . 这条线:

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

    应该替换为:

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

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

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

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

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

  • 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;
    

相关问题