typedef fixed length array typedef fixed length array c c

typedef fixed length array


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.


You want

typedef char type24[3];

C type declarations are strange that way. You put the type exactly where the variable name would go if you were declaring a variable of that type.


From R..'s answer:

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.

Users who don't see that it's an array will most likely write something like this (which fails):

#include <stdio.h>typedef int twoInts[2];void print(twoInts *twoIntsPtr);void intermediate (twoInts twoIntsAppearsByValue);int main () {    twoInts a;    a[0] = 0;    a[1] = 1;    print(&a);    intermediate(a);    return 0;}void intermediate(twoInts b) {    print(&b);}void print(twoInts *c){    printf("%d\n%d\n", (*c)[0], (*c)[1]);}

It will compile with the following warnings:

In function ‘intermediate’:warning: passing argument 1 of ‘print’ from incompatible pointer type [enabled by default]    print(&b);     ^note: expected ‘int (*)[2]’ but argument is of type ‘int **’    void print(twoInts *twoIntsPtr);         ^

And produces the following output:

01-45330897632767