在结构中使用typedef枚举

在结构中使用typedef枚举

本文介绍了在结构中使用typedef枚举,避免类型混合警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和C99合作。我的编译器是IAR Embedded工作台,但我认为这个问题对于其他编译器也是有效的。



我有一个typedef枚举,其中有几个项目,我添加了一个元素到新类型的结构

  typedef枚举
{
foo1,
foo2
} foo_t;

typedef struct
{
foo_t my_foo;
...
} bar_t;

现在我要创建一个bar_t的实例,并将其所有内存初始化为0。 >

  bar_t bar = {0u}; 

这会产生一个警告,我混合枚举与另一种类型。 IAR具体警告号码为Pe188。它编译并且工作正常,因为在一天结束时枚举是一个无符号的int。但是我想避免一千个噩梦的警告。初始化将类型枚举为0的结构类型的一个干净的方法是为了?为了参数,我们假设bar_t有很多成员 - 我想要将它们全部设置为0.我宁可不输入如下内容:

  bar_t bar = {foo1,0u,some_symbol ,...,0u}; 

编辑:额外的注意事项:我正在遵守MISRA。所以如果一个解决方法会违反MISRA,它只是为我移动问题。

解决方案

如果你真的,想要将结构的所有内存初始化为0,然后拼写为

  memset(& bar,0,sizeof(bar_t)) ; 

这实际上是很常见的,甚至往往工作,但从技术上讲,大多数人实际上是不正确的想。不能保证为大多数元素类型做正确的事情,因为C比许多人考虑不同类型的值的表示以及各种位模式的含义更少的保证。



初始化聚合对象(如结构体)的正确方法就好像为每个元素分配值零一样,但您始终遵循的方式是简单的

  bar_t bar = {0}; 

(不需要'u'后缀)。有一个很可能的变体可以避免编译器的警告,虽然我不会真的期待这样。



Ashalynd给出了我认为最正确的答案(即,

  bar_t bar = {foo1}; 

)。如果这样违反了您必须遵守的代码约定,那么也许您可以重新组织您的 struct ,以便第一个元素是除之外的任何类型枚举类型。


I am working with C99. My compiler is IAR Embedded workbench but I assume this question will be valid for some other compilers too.

I have a typedef enum with a few items in it and I added an element to a struct of that new type

typedef enum
{
    foo1,
    foo2
} foo_t;

typedef struct
{
    foo_t my_foo;
    ...
} bar_t;

Now I want to create an instance of bar_t and initialize all of its memory to 0.

bar_t bar = { 0u };

This generates a warning that I mixing an enumerated with another type. The IAR specific warning number is Pe188. It compiles and works just fine since an enum is an unsigned int at the end of the day. But I'd like to avoid a thousand naggy warnings. What's a clean way to initialize struct types that have enumerated types in them to 0?

for the sake of argument lets assume bar_t has a lot of members - I want to just set them all to 0. I'd rather not type out something like this:

bar_t bar = { foo1, 0u, some_symbol,... , 0u};

EDIT: Extra note: I am complying with MISRA. So if a workaround is going to violate MISRA it just moves the problem for me. I'll get nagged by the MISRA checker instead.

解决方案

If you really, literally want to initialize all the memory of a struct to 0, then that's spelled

memset(&bar, 0, sizeof(bar_t));

That's actually fairly common, and it even tends to work, but technically it's incorrect for what most people actually want. It is not guaranteed to do the right thing for most element types, because C makes fewer guarantees than many people think about the representations of values of different types, and about the meaning of various bit patterns.

The correct way to initialize an aggregate object (such as a struct) as if assigning the value zero to every element is what you started with, though the canonical way to write it is simply

bar_t bar = { 0 };

(no need for the 'u' suffix). There is a remote possibility that that variant would avoid the compiler warning, though I wouldn't really expect so.

Ashalynd gives the answer I think is most correct (that is,

bar_t bar = { foo1 };

). If that somehow violates code conventions with which you must comply, then perhaps you could reorganize your struct so that the first element is of any type other than an enum type.

这篇关于在结构中使用typedef枚举,避免类型混合警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:11