Is this valid?

  • Thread starter Colin JN Breame
  • Start date
C

Colin JN Breame

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[0]=0; d[1]=1; d[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[0], d[1], d[2]);
}
}
 
A

Andrey Tarasevich

Colin said:
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[0]=0; d[1]=1; d[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[0], d[1], d[2]);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top