2d array call in a function

Discussion in 'C++' started by axcytz@gmail.com, Oct 23, 2013.

  1. Guest

    Hi all,

    I have a 2d array that I declared as:

    int array[3][3] ={{1,2,3},{1,3,5},{3,2,5}};
    int InitialArray[3] = {..};
    int Result;

    I also use this in a function:

    int Calculate(int myarray[], int array[][3], int result)
    {
    for(int i=0; i<3 ;i++)
    {
    for(int j=0; j<i+1 ;j++)
    result += array[myarray][myarray[j]];
    }
    }

    In main, I call it as

    Calculate(InitialArray, array, Result);

    I get some errors because of the 2d array in function. How should i fix this?

    expected primary-expression before â]â token
    array bound is not an integer constant


    Thanks in advance!
    , Oct 23, 2013
    #1
    1. Advertising

  2. On Tue, 22 Oct 2013 18:15:10 -0700 (PDT), wrote:

    >Hi all,
    >
    >I have a 2d array that I declared as:
    >
    >int array[3][3] ={{1,2,3},{1,3,5},{3,2,5}};
    >int InitialArray[3] = {..};
    >int Result;
    >
    >I also use this in a function:
    >
    >int Calculate(int myarray[], int array[][3], int result)
    >{
    > for(int i=0; i<3 ;i++)
    > {
    > for(int j=0; j<i+1 ;j++)
    > result += array[myarray][myarray[j]];
    > }
    >}
    >
    >In main, I call it as
    >
    >Calculate(InitialArray, array, Result);
    >
    >I get some errors because of the 2d array in function. How should i fix this?
    >
    >expected primary-expression before â]â token
    >array bound is not an integer constant


    It would help a whole bunch if you posted your actual code (use cut
    and paste, do not retype) and the complete text of the error message.
    You might also mention which compiler you are using.

    What happens if you put the actual first dimension in the second
    parameter?
    What happens if you change the second parameter to the equivalent
    int(*array)[3]?

    --
    Remove del for email
    Barry Schwarz, Oct 23, 2013
    #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. grbgooglefan
    Replies:
    2
    Views:
    418
    Pascal Bourguignon
    Jan 30, 2008
  2. grbgooglefan
    Replies:
    4
    Views:
    438
    Kenny McCormack
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    0
    Views:
    389
    grbgooglefan
    Jan 30, 2008
  4. Alok
    Replies:
    3
    Views:
    247
  5. THAKUR PRASHANT SINGH

    Class Function call vs Normal Function call

    THAKUR PRASHANT SINGH, Feb 26, 2010, in forum: Ruby
    Replies:
    7
    Views:
    186
    THAKUR PRASHANT SINGH
    Feb 27, 2010
Loading...

Share This Page