anurag18294
anurag18294

Reputation: 99

calling a 2-dimensional matrix in a function call

while calling and defining a function how to use a 2-dimensional matrix in that function?

Upvotes: 0

Views: 184

Answers (2)

Mark B
Mark B

Reputation: 96261

Use a vector of vectors, for example: std::vector<std::vector<int> >. You can pass this by reference, const or not depending on whether you need to modify the values in the matrix.

Upvotes: 0

Jesse Naugher
Jesse Naugher

Reputation: 9820

C++ doesn't care about bounds, but it needs to compute the memory address given the subscripts (see below). To do this it needs to know the row width (number of columns). Therefore formal 2-dimensional array parameters must be declared with the row size, altho the number of rows may be omitted. For example,

void clearBoard(ticTacToeBoard[][3]) {
   . . .
}

(info from http://www.fredosaurus.com/notes-cpp/arrayptr/22twodim.html)

Upvotes: 1

Related Questions