读书人

结构体初始化的有关问题

发布时间: 2012-04-14 17:14:21 作者: rapoo

求助,结构体初始化的问题
MPlayer中结构体的初始化,有点看不太懂,望大虾帮忙解答
AVOutputFormat ipod_muxer = {
"ipod",
NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
"application/mp4",
"m4v,m4a",
sizeof(MOVContext),
CODEC_ID_AAC,
CODEC_ID_H264,
mov_write_header,
mov_write_packet,
mov_write_trailer,
.flags = AVFMT_GLOBALHEADER, //疑问
.codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0}, //疑问
};


结构体定义如下:
typedef struct AVOutputFormat {
const char *name;
/**
* Descriptive name for the format, meant to be more human-readable
* than \p name. You \e should use the NULL_IF_CONFIG_SMALL() macro
* to define it.
*/
const char *long_name;
const char *mime_type;
const char *extensions; /**< comma-separated filename extensions */
/** Size of private data so that it can be allocated in the wrapper. */
int priv_data_size;
/* output support */
enum CodecID audio_codec; /**< default audio codec */
enum CodecID video_codec; /**< default video codec */
int (*write_header)(struct AVFormatContext *);
int (*write_packet)(struct AVFormatContext *, AVPacket *pkt);
int (*write_trailer)(struct AVFormatContext *);
/** can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER */
int flags;
/** Currently only used to set pixel format if not YUV420P. */
int (*set_parameters)(struct AVFormatContext *, AVFormatParameters *);
int (*interleave_packet)(struct AVFormatContext *, AVPacket *out,
AVPacket *in, int flush);

/**
* List of supported codec_id-codec_tag pairs, ordered by "better
* choice first". The arrays are all CODEC_ID_NONE terminated.
*/
const struct AVCodecTag * const *codec_tag;

enum CodecID subtitle_codec; /**< default subtitle codec */

/* private fields */
struct AVOutputFormat *next;
} AVOutputFormat;


[解决办法]
一般初始化都是按顺序赋值的,但是linux提供如lz所列的初始化方式,可以随意对结构体的成员在任意位置赋值
[解决办法]
楼主请看c99标准:
http://blog.csdn.net/phlexii/archive/2006/06/30/855263.aspx
其中提到:
11、指定的初始化符
  C99中,该特性对经常使用稀疏数组的程序员十分有用。指定的初始化符通常有两种用法:用于数组,以及用于结构和联合。用于数组的格式:[index] = vol; 其中,index表示数组的下标,vol表示本数组元素的初始化值。
  例如: int x[10] = {[0] = 10, [5] = 30}; 其中只有x[0]和x[5]得到了初始化.用于结构或联合的格式如下:
  member-name(成员名称)
  对结构进行指定的初始化时,允许采用简单的方法对结构中的指定成员进行初始化。
  例如: struct example{ int k, m, n; } object = {m = 10,n = 200};
  其中,没有初始化k。对结构成员进行初始化的顺序没有限制。

读书人网 >C语言

热点推荐