user3485137
user3485137

Reputation: 9

2-Dimensional Arrays

I've looked at other posts regarding to this problem but can't really seem to apply it to my situation particularly this post: Passing a 2D array to a C++ function

int compute(int size, int graph[][size], int u, int v)

and get the following error:

candidate function not viable: no known conversion from
  'int [size][size]' to 'int (*)[size]' for 2nd argument
int compute(int size, int graph[][size], int u, int v)

When I call this function in my main method I do it the following way:

compute(size,matrix,0,size-1)

Any suggestions as what my problem is?

Upvotes: 0

Views: 492

Answers (2)

nwp
nwp

Reputation: 9991

template<size_t sizex, size_t sizey>
int compute(int (&graph)[sizex][sizey], int u, int v);

This behaves the way people expect, for example sizeof returns the correct size without needing to worry about tricky pointer conversions. It will also only let you pass 2d-Arrays, not pointers.

Upvotes: 1

jsantander
jsantander

Reputation: 5102

The following worked for me on g++

const int SIZE=3;
int compute(int s, int graph[][SIZE], int u, int v) {
        return 0;
}


int main(int argc,char **argv) {
    int matrix[SIZE][SIZE];
    compute(SIZE,matrix,0,SIZE-1);
}

Note that SIZE is an integer constant, and is not to be confused with the first parameter of compute.

Upvotes: 1

Related Questions