结构体与指针

struct s_options opt;//此时结构体内的数据都是一个随机数

struct s_options *opts = (struct s_options*)malloc( sizeof(struct s_options) * 8 );//此时各元素为默认初始化值,例如int就是0

一、结构体的定义方法

1、直接定义

(1)为了以后再定义,这里只是声明

struct s_options {
  enum { OPT_FLAG=1,  OPT_INT,  OPT_DBL,  OPT_STR,
         OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR} type;
  char *label;
  char *arg;
  char *message;
};

(2)既声明了,又定义了,以后也可定义

struct s_options {
  enum { OPT_FLAG=1,  OPT_INT,  OPT_DBL,  OPT_STR,
         OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR} type;
  char *label;
  char *arg;
  char *message;
} opt;
(3)只定义一次,以后不需要定义了

struct  {
  enum { OPT_FLAG=1,  OPT_INT,  OPT_DBL,  OPT_STR,
         OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR} type;
  char *label;
  char *arg;
  char *message;
} opt;

2、使用typedef

(1)

typedef struct s_options {
  enum { OPT_FLAG=1,  OPT_INT,  OPT_DBL,  OPT_STR,
         OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR} type;
  char *label;
  char *arg;
  char *message;
}OPT;

 (2)可以省略s_options

typedef struct {
  enum { OPT_FLAG=1,  OPT_INT,  OPT_DBL,  OPT_STR,
         OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR} type;
  char *label;
  char *arg;
  char *message;
}OPT;

二、没有指针的情况

1、赋值:

      (1)初始化赋值:

struct s_options opt = {
    {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."};
       (2) 先声明再赋值:

struct s_options opt;//此时结构体内的数据都是一个随机数
opt.type = OPT_FLAG;
opt.label = "b";
opt.arg = "1"
opt.message  = "Print only the basis in report";

2、引用:

   opt.type  opt.label opt.arg opt.message   (&opt)->type (&opt)->label (&opt)->arg (&opt)->message


三、一维指针

1、赋值:

      (1)初始化赋值:

struct s_options options[] = {
    {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
    {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
    {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
    {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file"},
    {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
    {OPT_FLAG, "s", (char*)&statistics, "Print parser stats to standard output."},
    {OPT_FLAG, "x", (char*)&version, "Print the version number."},
    {OPT_FLAG,0,0,0}
  };
       (2) 先声明再赋值:

struct s_options options[];
(option+1)->label = "c";
或者option[1].label = "c"; 或者*(option+1).label = "c"

2、引用:

  (option+1)->lable就是c,option[1].lable就是c,*(option+1).label = "c"


2、常用的变成指针来处理:

1、赋值:

      (1)初始化赋值:

struct s_options options[] = {
    {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
    {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
    {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
    {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file"},
    {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
    {OPT_FLAG, "s", (char*)&statistics, "Print parser stats to standard output."},
    {OPT_FLAG, "x", (char*)&version, "Print the version number."},
    {OPT_FLAG,0,0,0}
  };
struct s_options *opts = options;

2、引用:

  (opts+1)->label就是c,opts[1].label就是c,*(opts+1).label


四、动态分配

1、赋值:

      (1)赋值://只有一个时 也同理 opt->label (*opt).label

struct s_options *opts  = (struct s_options*)malloc( sizeof(struct s_options) * 8 );//此时各元素为默认初始化值,例如int就是0
opts->label = "g"; 
(opts+1)->label = "s";
opts[0].label =  "g";
opts[1].label = "s";  
或者(*(opts)).label = "g"
 (*(opts+1)).label = "s"

2、引用:opts->label (opts+1)->label或者opts[0].label opts[1].label 或者(*(opts)).label (*(opts+1)).label

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值