Is this valid?

Discussion in 'C++' started by Colin JN Breame, Mar 1, 2004.

  1. 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]);
    }
    }
     
    Colin JN Breame, Mar 1, 2004
    #1
    1. Advertising

  2. 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[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]);
    > }
    > }


    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Mar 1, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aschel Kritsos

    Valid file types

    Aschel Kritsos, Nov 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    597
  2. Ajit
    Replies:
    1
    Views:
    3,613
    Victor Garcia Aprea [MVP]
    Apr 24, 2004
  3. CT

    vbc is not valid command

    CT, Oct 30, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    2,841
  4. Pial
    Replies:
    6
    Views:
    52,412
    Nassos
    Apr 28, 2005
  5. MM
    Replies:
    3
    Views:
    450
    Kai Harrekilde-Petersen
    Jun 19, 2004
Loading...

Share This Page