的typedef固定长度的数组 [英] typedef fixed length array

查看:215
本文介绍了的typedef固定长度的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须定义type.I正在使用的char [3] 重新present类型24位的数据。我可以的typedef 的char [3] type24 ?我试了一个code样本。我把的typedef的char [3] type24; 在我的头文件。编译器并没有抱怨。但是,当我定义的函数无效美孚(type24 VAL){} 在我的C文件,它没有抱怨。我想能够定义如 type24_to_int32(type24 VAL)而不是 type24_to_int32的(char值[3])。

I have to define a 24-bit data type.I am using char[3] to represent the type. Can I typedef char[3] to type24? I tried it in a code sample. I put typedef char[3] type24; in my header file. The compiler did not complain about it. But when I defined a function void foo(type24 val) {} in my C file, it did complain. I would like to be able to define functions like type24_to_int32(type24 val) instead of type24_to_int32(char value[3]).

推荐答案

的类型定义是

typedef char type24[3];

不过,这可能是一个非常糟糕的主意,因为由此产生的类型是数组类型,但它的用户不会看到它是一个数组类型。如果用作函数的参数,它会按引用传递,而不是值,的sizeof 为它就会是错误的。

有一个更好的解决办法。

A better solution would be

typedef struct type24 { char x[3]; } type24;

您可能还需要使用 unsigned char型而不是字符,因为后者具有实现定义的符号性

You probably also want to be using unsigned char instead of char, since the latter has implementation-defined signedness.

这篇关于的typedef固定长度的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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