Another Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Howard

    Another Question

    I'm working on my homework for C++, and have a problem. I have a
    multi-dimension array, Array1[5][4] that I can't define in the function
    declaration and function definition. I keep getting an error. I've tried
    the following:

    void Sum ( int [][] );

    I get an error with this, and also with:

    void Sum ( int [] );

    The compiler, Microsoft VC6 can't convert the two-dimensional array to a
    one-dimension array.

    What I'm I doing wrong? I can't find the answer in my notes or the book,
    they all deal with one-dimension arrays.

    John Howard


  • Jim Fischer

    #2
    Re: Another Question

    John Howard wrote:[color=blue]
    > I'm working on my homework for C++, and have a problem. I have a
    > multi-dimension array, Array1[5][4] that I can't define in the function
    > declaration and function definition. I keep getting an error. I've tried
    > the following:
    >
    > void Sum ( int [][] );
    >
    > I get an error with this, and also with:
    >
    > void Sum ( int [] );
    >
    > The compiler, Microsoft VC6 can't convert the two-dimensional array to a
    > one-dimension array.
    >
    > What I'm I doing wrong? I can't find the answer in my notes or the book,
    > they all deal with one-dimension arrays.[/color]

    The array declaration

    int a[3][2]

    declares an array variable 'a' that has 3 elements, where each element
    'a[n]' is an object of type 'int[2]':

    typeof(a[n]) == int[2]

    IOW, each element in 'a' is an "array object" consisting of 2 int
    subobjects:

    a[0] ::= [[int][int]] // int[2] object
    a[1] ::= [[int][int]] // int[2] object
    a[2] ::= [[int][int]] // int[2] object

    Recall that when you pass an array to (or return an array from) a
    function, what you are really doing is passing the address of the
    array's first element:

    void g1 (int *q, int size);
    void g2 (int q[], int size); // alternate "array argument" syntax

    int main() {
    int b[5];

    g1(b, 5); // i.e., g1(&b[0], 5);
    g2(b, 5); // i.e., g2(&b[0], 5);

    /* n.b.
    * typeof(b[0]) == int
    * typeof(&b[0]) == int*
    */
    }

    Since 'a[0]' is an object of type 'int[2]', the address of a's first
    element (i.e., '&a[0]') will be a pointer whose type is 'int(*)[2]':

    typeof(a[0]) == int[2]
    typeof(&a[0]) == int(*)[2]

    So to pass the address of array a's first element (i.e., &a[0]) into a
    function, you need to declare the function's arguments accordingly:

    <example>
    // n.b. In functions f1 and f2 below, the formal parameter 'p'
    // is a pointer to an 'int[2]' object.

    void f1 (int(*p)[2], int rows)
    {
    for (int r = 0; r < rows; ++r) {
    for (int c = 0; c < 2; ++c) {
    p[r][c] = 0;
    }
    }
    }

    // or, using the alternate "array argument" syntax,

    void f2 (int p[][2], int rows) { /* ... */ }

    int main()
    {
    int a[3][2];
    f1 (a, 3);
    f2 (a, 3);
    }
    </example>

    n.b. Applying the array indexing operator '[]' to an int[2] object lets
    you access the individual int subobjects within the int[2] object:

    typeof(p[r]) == int[2]
    typeof(p[r][c]) == int

    e.g.

    p[r] ::= [[567][222]]
    p[r][0] ::= 567
    p[r][1] ::= 222

    FWIW, these concepts carry over to all N-dimension arrays:

    void d1 (int (*parray)[4][3], int dim1);
    void d2 (int parray[][4][3], int dim1); // alternate syntax

    int c[5][4][3];

    d1 (c, 5); // i.e., d1(&c[0], 5);
    d2 (c, 5); // i.e., d2(&c[0], 5);

    typeof(c) == int[5][4][3]
    typeof(&c) == int(*)[5][4][3]

    typeof(c[i]) == int[4][3]
    typeof(&c[i]) == int(*)[4][3]

    typeof(c[i][j]) == int[3]
    typeof(&c[i][j]) == int(*)[3]

    typeof(c[i][j][k]) == int
    typeof(&c[i][j][k]) == int(*) == int*

    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link58 09{at}now.here. com


    Comment

    • Administratoren

      #3
      Re: Another Question

      hi,
      i would do it quick and dirty, if everything else fails:
      void Sum(void* lpBlabla)// prototype
      cast it back to int[][] in function Sum

      call it so:
      Sum((void*)&Sum[0][0]);




      -------- Original Message --------
      Subject: Another Question (18-Jul-2003 17:45)
      From: John Howard <jdhoward72@yah oo.com>
      To: comp.lang.c++
      [color=blue]
      > I'm working on my homework for C++, and have a problem. I have a
      > multi-dimension array, Array1[5][4] that I can't define in the function
      > declaration and function definition. I keep getting an error. I've tried
      > the following:
      >
      > void Sum ( int [][] );
      >
      > I get an error with this, and also with:
      >
      > void Sum ( int [] );
      >
      > The compiler, Microsoft VC6 can't convert the two-dimensional array to a
      > one-dimension array.
      >
      > What I'm I doing wrong? I can't find the answer in my notes or the book,
      > they all deal with one-dimension arrays.
      >
      > John Howard
      >
      >[/color]

      Comment

      • Karl Heinz Buchegger

        #4
        Re: Another Question



        Administratoren wrote:[color=blue]
        >
        > hi,
        > i would do it quick and dirty, if everything else fails:
        > void Sum(void* lpBlabla)// prototype
        > cast it back to int[][] in function Sum
        >
        > call it so:
        > Sum((void*)&Sum[0][0]);[/color]

        That's the worst advise one could give.
        Please refrain from giving such advises in
        the future.

        Thank you.

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        Working...