-2

The question is quite pedestrian. For instance, I have declared a 2D character array called arr[5][5]. While I am aware of the number of rows prevalent in the array, I wish to conceive a function capable of determining the number of rows by itself, which in essence is equivalent to its length. How does one approach this problem?

user438383
  • 5,716
  • 8
  • 28
  • 43
  • The problem has no solution in C. You need to pass the size(s) to the function along with the array. – pmg Sep 08 '21 at 14:40
  • 1
    Basically, you have to *tell* a function what the outer dimension is as a separate argument. Even if you have say `char arr[6][5]` as an argument, the compiler will ignore the `6`. The function actually receives a pointer, and the declaration tells the compiler how to index it. – Weather Vane Sep 08 '21 at 14:40
  • 1
    @WeatherVane, you can always pass a pointer to array. The function will see `char (*arr)[6][5]`, now none of the dimensions is ignored – tstanisl Sep 08 '21 at 14:44
  • 1
    If `arr` is an array, then `sizeof arr / sizeof arr[0]` is the number of elements. A 2-D array is an array of arrays, so the number of rows is the number elements of the outer array. It doesn't work if `arr` is a pointer because that would give the size of a pointer divided by the size of an element, which is not the number of rows. – Ian Abbott Sep 08 '21 at 14:47
  • @IanAbbott, it would be better saying that it returns garbage, rather than "not work". "Not work" is often understood as compilation error – tstanisl Sep 08 '21 at 14:50
  • Does this answer your question? [Is there a standard function in C that would return the length of an array?](https://stackoverflow.com/questions/1598773/is-there-a-standard-function-in-c-that-would-return-the-length-of-an-array) – tstanisl Sep 08 '21 at 15:41

1 Answers1

1

When you declare an array in C, you mention the dimensions. Generally those are the dimensions you need, but they might also be the maximum dimensions.

E.g. you declare a five by five array, in memory this becomes (you have no idea about the values X, they might be individually different):

X X X X X
X X X X X
X X X X X
X X X X X
X X X X X

Your program is filling in only a part of your array (let's say the first two by two array is filled with the value 5), so you get:

5 5 X X X
5 5 X X X
X X X X X
X X X X X
X X X X X

And now you would like to know which part of your array has been filled, but you have no idea of the values X.

One of the ways to handle this is to fill your array with default values, which cannot be used by your program (e.g. you know that your program will never use negative values, so as a default value you might use -1):

-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1

When your program fills your array, you get:

 5  5 -1 -1 -1
 5  5 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1
-1 -1 -1 -1 -1

You might check your array by looking for the first row (and/or column) which contains the value '-1'.

Dominique
  • 16,450
  • 15
  • 56
  • 112