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