Thread: Trying to pass 2d array into function,pulling my hair out!

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    1

    Trying to pass 2d array into function,pulling my hair out!

    Hi I am getting this error with my code as I try to pass a 2d array into a function :

    mymain.cpp:173: error: cannot convert `float (*)[((dimensions - 1) + 1)]' to `float**' for argument `1' to `float objfunc(float**)'])'

    The code is as follows:

    http://cpp.sourceforge.net/?show=4356

    The lines 0030, 173 and 282 are where the problems are,

    If anyone could be of any help i woule be extremely gratefull as I have been stuck on it for quite a while now. thanks in advance.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    I could be wrong here but I'm going to take a stab in the dark.
    Because you are passing an array of arrays or a pointer to a pointer, you need to specify where the array begins.
    try passing it like this
    Code:
    tempfitness=objfunc(arrPart[][0]);
    Or something like that.



    *sand_man closes his eyes and hopes he is right
    Last edited by sand_man; 03-23-2005 at 05:19 AM.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by tw0nk
    Hi I am getting this error with my code as I try to pass a 2d array into a function :

    If anyone could be of any help i woule be extremely gratefull as I have been stuck on it for quite a while now. thanks in advance.
    Program calculations that determine the address of an element of a two-dimensional array are different from those of an element an array of arrays.

    Suppose you have the following:

    Code:
      int ary2d[numrows][numcols];
    Then for ints i and j, the the program calculates the address of ary2d[i][j] like this (using pointer arithmetic for ints):

    address of ary2d[i][j] = (address of ary2d[0][0]) + i * numcols + j

    on the other hand, if you have this:

    Code:
      int **ppint;
    
      ppint = new int * [pnumrows];
    
      for (i = 0; i < pnumrows; i++) {
        ppint[i] = new int[pnumcols];
      }
    Then for ints i and j, the program calculates the address of ppint[i][j] like this (using pointer arithmetic for ints):

    address of ppint[i][j] = *(address of ppint[i]) + j


    Bottom line:

    For both cases the programmer can refer to elements by array terminology.

    Code:
    ppint[i][j] = xxx;
    ary2d[i][j] = xxx;
    Within the scope of the definitions of ppint and ary2d, there is no difference in how they are used (the compiler keeps track of how the addresses are calculated for the elements).

    But you can't make a single function that will handle both.

    Consider the following:

    One function is defined like this:

    Code:
    void func1 (int **abc, int row, int col)
    {
      int xxx = abc[row][col]
      // do something with it
    
    }
    Another function is defined like this

    Code:
    void func2(int xyz[][numcols], int row, int col)
    {
      int xxx = xyz[row][col];
      // do something with it
    }

    1. You cannot invoke func1 with an argument that is a 2d array

    2. You can invoke func1 with an argument that is an array of pointers to int, since that's semantically the same as a pointer to a pointer to an int.

    3. You cannot invoke func2 with an argument that is a pointer to a pointer to int.

    4. You can invoke func2 that is a pointer to an array[numcols] of int. (Note: this is (*p)[ncols], not *p[ncols])

    5. Definition and prototype of func2 must explicitly have numcols, where numcols is a const integral type. That is to say that, for function definitions with 2D array arguments, the first dimension of a 2D array is free, the second must be specified.

    Regards,

    Dave
    Last edited by Dave Evans; 03-23-2005 at 10:40 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > *sand_man closes his eyes and hopes he is right
    You're not.

    It's really quite simple, paste the declaration of the array you want to pass
    Say
    float arrPart[particles][dimensions];

    Into the prototype of the function you want to pass the array to, say
    float objfunc(float arrPart[particles][dimensions]); //declaration of function

    And to call the function, you simply use the name of the array (you got that bit right)
    tempfitness=objfunc(arrPart);

    The array access code inside the function looks EXACTLY the same as the array access code would look if you had direct access to the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  3. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM