341008 341008 - 1 year ago 133
C Question

typedef fixed length array

I have to define a 24-bit data type.I am using

to represent the type. Can I typedef
? 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])

R.. R..
Answer Source

The typedef would be

typedef char type24[3];

However, this is probably a very bad idea, because the resulting type is an array type, but users of it won't see that it's an array type. If used as a function argument, it will be passed by reference, not by value, and the sizeof for it will then be wrong.

A better solution would be

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download