Colin JN Breame wrote:
> I'm trying to figure out whether this way of passing arrays is valid. It
> seems to work however, I'm experiencing problems with another program that
> might be related.
>
> If this is not valid, how else can it be done? Is an array of
> double[5][3]==double[][3]==double[][]==double** ?
In parameter declaration 'double[5][3]' is equivalent to 'double[][3]',
but not to 'double[][]' (which is incorrect syntax) or 'double**'.
'double[5][3]' is equivalent to 'double[][3]', which is equivalent to
'double(*)[3]' - a pointer to an array of 3 'double's.
> #include <stdio.h>
>
> int num;
>
> void set(double d[][3]) {
> for (int i=0; i<num; i++) {
> d[i][0]=0; d[i][1]=1; d[i][2]=2;
> }
> }
>
> int main(int argc, char *argv[]) {
> scanf("%d", &num);
>
> double d[num][3];
This is not legal C++. 'num' is not an Integral Constant Expression
(ICE) and you can't use non-ICE array sizes in C++. If this code is
supposed to work with your compiler but doesn't, consult your compiler's
documentation or try asking in the compiler-specific newsgroup.
> set(d);
> for (int i=0; i<num; i++) {
> printf("%f %f %f\n", d[i][0], d[i][1], d[i][2]);
> }
> }
--
Best regards,
Andrey Tarasevich
|