Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this valid?

Reply
Thread Tools

Is this valid?

 
 
Colin JN Breame
Guest
Posts: n/a
 
      03-01-2004
Hi,

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** ?

Thanks
Colin


#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];
set(d);
for (int i=0; i<num; i++) {
printf("%f %f %f\n", d[i][0], d[i][1], d[i][2]);
}
}
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-01-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57