0

At the beginning, my code looked like this,and I compiled the code using 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;
};

And it sends the message that unknown type name 'pcb'. Then I modify the code according to what I found in the Internet, and my revised code is as follows.

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;

But new errors occurred; the compiler sent the message about unknown type name 'pnode' to me. I don't know how to using my structure variable when I define a structure. Please give me some tips.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Peiqin
  • 79
  • 1
  • 7
  • 1
    Related to this http://stackoverflow.com/q/588623/694576 if not a duplicate to it. – alk May 15 '16 at 07:31

3 Answers3

1

Keyword struct is required in C to declare variable having structure type.

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

You can use forward declaration to avoid writing many 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;
};
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

You should write

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

Or

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;
};
  • In the second example, it would be sufficient to use the two `typedef` lines; the `struct xyz;` lines aren't needed (though they do no harm other than add to the number of lines to be processed). – Jonathan Leffler May 15 '16 at 07:41
1

You can reference the uasge of the keyword 'typedef', the type definition is only allowed to be used after typedef struct pnode pnode; has been defined.

So you have two choices to fix the error.

  1. Define typedef struct pnode pnode; before you use the type pnode *sub;.
  2. Modify your typedef struct pnode to:

    typedef struct pnode{ pcb *node; struct pnode *sub;
    struct pnode *brother; struct pnode *next; } pnode;

CWLiu
  • 3,913
  • 1
  • 10
  • 14