0

Directions given: Write a function int getLength(int grid[][6]) that computes the number of elements contained within the 2-D array.

My first question is: How do we pass an array into a function? I get an error when I try to do that.

    int x[][2] = {{ 1, 2 }, { 2, 3 }};
    int getLength(x);

Error: a value of type "int (*)[2]" cannot be used to initialize an entity of type "int"

============================================================

Also, is this as simple as just using sizeof() like so?:

int getLength(int grid[][6]){
     cout << sizeof(grid);
     return sizeof(grid);
}
Rimoun
  • 455
  • 1
  • 9
  • 16
  • 1
    `sizeof` does not work because arrays cannot be passed by value (the function receives a pointer and the [syntax is deceptive](http://stackoverflow.com/questions/22677415/why-do-c-and-c-compilers-allow-array-lengths-in-function-signatures-when-they/22677793#22677793)). You have to actually pass the length as another parameter to the function. – M.M Feb 03 '15 at 09:02
  • 3
    The "directions given" is impossible, unless there is further information that the contents of the array contain an end marker, or a special entry for the length, or whatever – M.M Feb 03 '15 at 09:04
  • You might avoid having 2D arrays, and have some `class` (e.g. with your `operator[]` ) – Basile Starynkevitch Feb 03 '15 at 09:04

4 Answers4

2

That's impossible. When an array is passed to a function, it decays to a pointer, leaving no way to retrieve the array size.

You could use a template to extract the size from the array type:

template <size_t N, size_t M>
size_t getLength(int (&)[N][M]) {return N*M;}

By passing the array by reference, it retains its type, from which the template arguments can be deduced automatically to give the return value.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

If you're passing a 2-D array to function getLength() as

 int x[][2];
 getLength(x);

the function declaration should be:

 getLength(int x[][2]) { .. }

In the above declaration number of rows need not be specified as we are not allocating the memory for the array hence can be ignored. Number of columns is required for the dimension of the array.

The size of 2D array can be calculated as:

ROW * COL * sizeof x[0][0]
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
0
#include <iostream>
using namespace std;
int getLength(int x[][2]);
void main(){
    int x[][2] = { { 1, 2 }, { 2, 3 } };
    int y = getLength(x);
    cout << y << endl;
}
int getLength(int x[][2]){
    cout << sizeof(x) << endl;
    for (int i = 0; i < 2; i++){
        for (int j = 0; j < 2; j++)
            cout << x[i][j]<<" ";
        cout << endl;
    }
    return sizeof(x);
}

This result is

4

1 2

2 3

4

Isn't it your interest?

Lightstar
  • 183
  • 3
  • 12
  • 1
    The result happens to be 4 because you're running on a 32-bit operating system, where that's the number of bytes in a pointer. It's got nothing to do with the array size. – Mike Seymour Feb 03 '15 at 09:11
0

If you task is to send 2D-array of any size to function just sent pointer and two sizes. But if you want to measure size by such function, be careful sizeof(grid); will calculate a size of pointer 4 or 8 (i.e. 32 or 64 bit depending on your system) but not size of array.

VolAnd
  • 6,367
  • 3
  • 25
  • 43