typedef array

Hi,, can i typedef array?

 
  typedef double account[4];

will this be like array of 4 doubles?
Yes, you can. That creates an alias where any object of type account will actually be of type double[4].
Note that array typedefs are seen to be a bad idea (at least a bit evil!):

From "typedef fixed length array"
http://stackoverflow.com/questions/4523497/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;

In C++ you don't have to go to the trouble of using the typedef when declaring the struct type (in C this is done to avoid having to type struct all over the place). So you could do something like:

1
2
3
struct Account {
    double values[4]; // or whatever
};


It does of course mean you have to access the values like this:

1
2
3
Account acc1 = {0};

acc1.values[0] = 100.23;


Andy
Last edited on
Topic archived. No new replies allowed.