3

When I declare or just write a function which takes a 2-dimensional char-array in C, Visual Studio tells me I have to put a value in the columns parameter, for example:

void board(char mat[][MAX_COLUMNS]);

so my question is why do I even need to tell C one dimension of the 2 dimensional array, and why does it have to be the number of columns specifically.

0

4 Answers 4

2

Because arrays are not first class objects in C. When you pass an array to a function, it decays to a pointer and the callee cannot guess the size. For a 1D array, it still allows to access elements through pointer arithmetics. But for a 2D array (an array of array) pointer arithmetics require that the size of the second level object (here a row) is known. That is the reason why the number of columns must be explicit.

In addition, Microsoft C does not support Variable Length Array, so the number of columns must be a constant.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Though when you said "second level object" i thought you ment the column, but you ment the row. why?
@Idan: in C a 2D array is an array of rows, and the size of a row is given by the number of columns.
1

Weather Vane pointed out well.

Plus, if you want to circumvent that restriction, use this prototype:

void board(char *mat, int rows, int columns);

And you can access it by this expression.

mat[i*columns+j]

when you want to access ith row jth column element.

Hope it helped!

2 Comments

NO!. A 2D array is not an array of pointers. NEVER use a pointer to pointer to access a 2D array.
@SergeBallesta OWW, maybe I confused with java? sorry and thank you!!@@
0
void board(char mat[][MAX_COLUMNS]);

is equivalent to

void board(char (*mat)[MAX_COLUMNS]);

with char (*mat)[MAX_COLUMNS] being the type your 2D-array is decayed to when passed to board(): To a pointer to its 1st element, as done to any array passed to a function.

Comments

0

Suppose you have an array

char arr[3][4];

and define the function as

void board(char mat[][4])

The array decays to a pointer, so if the function wants to access mat[2][1] then the offset from the pointer will be row x width + column elements, so 2 * 4 + 1 = 9. Note that arrays are always contiguous, no matter how many dimensions.

But if you define the function as

void board(char mat[][])

then there is no information except the pointer, and the compiler has no idea how to index the array.

The reason the dimension given has to be the number of columns, is because that is the way the array is laid out in memory, row by row.

3 Comments

another question: why when i summon the same function in itslef (recursion) it tells me I have to put also the number of rows?
Show how you are passing the argument to the recursing function within itself.
... when you recurse the function, it should be with board(mat); and of course there must be an exit condition to prevent infinite recursion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.