嵌套结构的访问元素

时间:2012-08-09 19:54:45

标签: c struct

在C中,是否可以将结构嵌套在另一个结构中?我如何访问内部结构的字段?以kinfo_proc的结构为例:

struct kinfo_proc {
  struct  extern_proc kp_proc;      /* proc structure */
  struct  eproc {
    struct  proc *e_paddr;    /* address of proc */
    struct  session *e_sess;  /* session pointer */
    struct  _pcred e_pcred;   /* process credentials */
    struct  _ucred e_ucred;   /* current credentials */
    struct   vmspace e_vm;    /* address space */
    pid_t e_ppid;     /* parent process id */
    pid_t e_pgid;     /* process group id */
    short e_jobc;     /* job control counter */
    dev_t e_tdev;     /* controlling tty dev */
    pid_t e_tpgid;    /* tty process group id */
    struct  session *e_tsess; /* tty session pointer */
#define WMESGLEN  7
    char  e_wmesg[WMESGLEN+1];  /* wchan message */
    segsz_t e_xsize;    /* text size */
    short e_xrssize;    /* text rss */
    short e_xccount;    /* text references */
    short e_xswrss;
    int32_t e_flag;
#define EPROC_CTTY  0x01  /* controlling tty vnode active */
#define EPROC_SLEADER 0x02  /* session leader */
#define COMAPT_MAXLOGNAME 12
    char  e_login[COMAPT_MAXLOGNAME]; /* short setlogin() name */
#if CONFIG_LCTX
    pid_t e_lcid;
    int32_t e_spare[3];
#else
    int32_t e_spare[4];
#endif
  } kp_eproc;
};

我如何尝试获取新制作的kinfo_proc实例的e_ppid?

1 个答案:

答案 0 :(得分:4)

您将创建类似的内容:

struct kinfo_proc test1;
test1.kp_eproc.e_ppid /// Access it like this.