tags:

views:

71

answers:

3

Hi,

when defining new data types in C, one can do

    typedef double BYTE;

so later it is possible to do

BYTE length;

etc

I would like to do something like

typedef double[30][30][10] mymatrix;

so later I do

mymatrix AA[10];

so I have 10 matrices of type mymatrix, and I can access them through AA[0], AA[1], etc

Anyway doing this with the GNU C compiler I get errors like

error: expected unqualified-id before '[' token

What am I doing wrong or how could I achieve my objective?

Thanks

+3  A: 

Follow "declaration looks like use" C idea:

typedef double mymatrix[30][30][10];
Nikolai N Fetissov
one last question. now it works and I can do: mymatrix AA[10], how do I access the elements of each matrix? I tried, for instance, for the first matrix, AA[0][1][2][3], is this right?
Werner
You only have **3 dimensions** here, use three indexes: `AA[i][j][k]`.
Nikolai N Fetissov
@Nikolai: he's declaring a 10-element array of type `mymatrix`, which is a 30x30x10 array of `double`; thus, he's ultimately working with four dimensions.
John Bode
@John, thanks (bad eyes :) - so right `AA[matrix_index][i][j][k]`.
Nikolai N Fetissov
+2  A: 

The simple answer is define an object named & declared as you want, then stick typedef in front:

double mymatrix[30][30][10] ; // defines a 3-d array.


typdef double mymatrix[30][30][10] ; // defines a 3-d array type

mymatrix  matrix;
James Curran
+1  A: 
typedef double mymatrix[30][30][10];
abenthy
Oh, seems like Nikolai was a little bit quicker at typing ;-)
abenthy