You can't use ftmat[][]. If you want the compiler to perform the
initialization, then you must specify the size of each dimension
except the first. If you wish, you can let the compiler determine the
size of the first dimension by counting the number of initialization
groups exist for the array.
Euh, so If I understand it correctly I can't use varname[][] within a
struct. The size has to be known at compile time.
Right. In a struct you can't even use varname[][N] where N is a
suitable (integer constant) value -- you must specify both bounds, or
more generally all -- except in C99 as the last element where it
becomes a Flexible Array Member. The feature of automatically
determining the first/outer bound (only) from the initializer applies
only to 'standalone' variables.
I have a lot of filters like that in the program I am writing but I
would like to group these [different sized] matrix's somehow and
access them like that.
Like the struct give above and;
struct filter ftab[] = {
{ 1.0, 0.0, 3, 3, { {0,0,0}, {0,1,0}, {0,0,0} } }, /* edge */
{ 1.0, 3.1, 4, 4, { {1,0,1.0}, {0,1,0,0}, {0,1,0,0} } } /* blur */
..... etc....
};
If the sizes vary only within some modest range, like 2 to 5 or even 1
to 10, and you don't have a huge number of them, you could simply
declare and hence allocate the maximum you ever need, but initialize
and then use only the needed parts of each one. This wastes some
space, but if amounts to only say 1-millionth of a cent of RAM, fine.
If they vary a lot, like up to many thousands or millions, you
probably want to use (in the struct) pointers to separately, and
perhaps dynamically, allocated spaces of the appropriate differing
sizes. If you want to vary both bounds, or more generally more than
one, you need to use intermediate pointers -- which in C can be
accessed in code as if they were multi-dim arrays, i.e. foo[x][y] etc.
-- BUT THEY AREN'T, see section 6 of the FAQ, and don't believe any
one who tells you that e.g. int [] [] and int * * are the same.
Well, or do the subscript (stride) calculation yourself and actually
code a 1-D array.
Also remember in these cases that assigning the struct copies only the
pointer, not its target(s). If you actually want a "deep" copy (that
is a new, safe, separate copy of the data) you must code that
explicitly. Similarly if you write out to a file and read back in, or
<offtopic=nonstandard> send to another process/program </>, you need
to explicitly handle the target(s) rather than the pointer(s).
- David.Thompson1 at worldnet.att.net