值分配给结构在的C类型的常量 [英] Assign value to struct type constants in C

查看:88
本文介绍了值分配给结构在的C类型的常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code,我的猜测是,值分配给恒结构

I have following code which I guess is assigning a value to constant struct

在头文件:

struct madStruct {
    uint8_t code;
    uint8_t cluster;
};
typedef struct madStruct MadStruct;

C 文件

const MadStruct madStructConst = {
    .code = 0x00,
    .cluster = 0x01,
};

我想知道什么呢这code咋办呢?

I would like to know what what does this code supposed to do?

这code不会在Visual Studio C ++ 2010编译,我怎么可以转换,所以我可以在这两个MingW平台和Visual Studio编译C ++ 2010?

This code does not compile in Visual Studio C++ 2010, how can I convert it so I can compile in both MingW and Visual Studio C++ 2010?

推荐答案

的语法在C99引入,并允许在初始化(即 code被明确指定成员的名称 .cluster 被称为代号的)。初始化分配值 0×00 code 成员和值 0×01 的群集成员。

The syntax was introduced in C99 and allows the name of the members to be explicitly specified at initialisation (the .code and .cluster are known as designators). The initialisation assigns the value 0x00 to the code member and value 0x01 to the cluster member.

VC只支持C89,所以编图失败。由于结构只有两个成员,都被初始化,您可以替换为初始化:

VC only supports C89, so compiliation fails. As the struct only has two members and both are being initialised you can replace the initialisation with:

const MadStruct madStructConst = { 0x00, 0x01 };

无指示符成员被初始化,在该命令指定的值,该成员在结构定义。在这种情况下 code 分配 0×00 的群集分配 0×01 ,一样的指示符初始化。

without the designators the members are initialised with the specified values in the order that the members are defined in the struct. In this case code is assigned 0x00 and cluster is assigned 0x01, the same as the initialisation with the designators.

这篇关于值分配给结构在的C类型的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆