一开始,我的代码看起来像这样,我使用gcc编译代码
struct pcb{
int pid; /* process id */
int ppid; /* parent process id */
int prio; /* priority */
};
/* process node */
struct pnode{
pcb *node;
pnode *sub;
pnode *brother;
pnode *next;
};
它会发送未知类型名称为“pcb
”的消息。然后我根据我在互联网上找到的内容修改代码,修改后的代码如下。
typedef struct pcb{
int pid; /* process id */
int ppid; /* parent process id */
int prio; /* priority */
int state; /* state */
int lasttime; /* last execute time */
int tottime; /* totle execute time */
} pcb;
/* process node */
typedef struct pnode{
pcb *node;
pnode *sub;
pnode *brother;
pnode *next;
} pnode;
但是发生了新的错误;编译器向我发送了有关未知类型名称“pnode
”的消息。我定义一个结构时不知道如何使用我的结构变量。请给我一些提示。
答案 0 :(得分:1)
C中需要关键字struct
来声明具有结构类型的变量。
struct pcb{
int pid; /* process id */
int ppid; /* parent process id */
int prio; /* priority */
};
/* process node */
struct pnode{
struct pcb *node;
struct pnode *sub;
struct pnode *brother;
struct pnode *next;
};
您可以使用前向声明来避免编写许多struct
。
typedef struct pcb{
int pid; /* process id */
int ppid; /* parent process id */
int prio; /* priority */
int state; /* state */
int lasttime; /* last execute time */
int tottime; /* totle execute time */
} pcb;
/* process node */
typedef struct pnode pnode;
struct pnode{
pcb *node;
pnode *sub;
pnode *brother;
pnode *next;
};
答案 1 :(得分:1)
你应该写
struct pcb{
int pid; /* process id */
int ppid; /* parent process id */
int prio; /* priority */
};
/* process node */
struct pnode{
struct pcb *node;
struct pnode *sub;
struct pnode *brother;
struct pnode *next;
};
或
struct pcb;
typedef struct pcb pcb_t;
struct pnode;
typedef struct pnode pnode_t;
struct pcb{
int pid; /* process id */
int ppid; /* parent process id */
int prio; /* priority */
};
/* process node */
struct pnode{
pcb_t *node;
pnode_t *sub;
pnode_t *brother;
pnode_t *next;
};
答案 2 :(得分:1)
您可以引用uasge of the keyword 'typedef',只有在定义typedef struct pnode pnode;
后才能使用类型定义。
所以你有两个选择来修复错误。
typedef struct pnode pnode;
之前定义pnode *sub;
。将typedef struct pnode
修改为:
typedef struct pnode{
pcb *node;
struct pnode *sub;
struct pnode *brother;
struct pnode *next;
} pnode;