1

While passing a 2-Dimensional array we have to specify the the column. eg:

void funtion1(a[])// works
{
}
void function2(a[][4])//works
{
}
void function3(a[][])//doesn't work
{
}

What could be the possible reasons that the function3 is considered an incorrect definition. Is there a different way to define function3 so that we can leave both row and column blank. Reading some replies: Can you explain how x[n] and x[] are different?. I guess the former represents a specific array position and the latter is unspecified array. More explanation will be deeply appreciated.

  • 1
    Yeah, it's called `std::vector`. –  Mar 15 '13 at 14:36
  • Why are you passing around data? – Peter Wood Mar 15 '13 at 14:38
  • `boost::multi_array` might also work for you. – hannes Mar 15 '13 at 14:39
  • `x[]` and `x[n]` are _fundamentally different things_ in C. Explaining why is not a topic for a question, but for a chapter in a good book on C. In C++, both are advanced topic and should be hidden out of sight deep in library code. – Jan Hudec Mar 15 '13 at 14:40
  • Ad edit: I wrote in the comment that it is a topic for a book chapter and not a reply. There is a list of recommended reading for C and C++ somewhere on stack overflow, just search for it. – Jan Hudec Mar 15 '13 at 14:46
  • 1
    Almost *ever* question about basic arrays in C or C++ can be answered if you review [this question](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c/4810676#4810676). I suggest you do so. Awhile ago I answered pretty much this [same question](http://stackoverflow.com/questions/14029930/passing-multidimesional-array-in-c-c/14030037#14030037), in case you don't get enough bombardment from the first answer. – WhozCraig Mar 15 '13 at 14:47
  • @WhozCraig Thank you soo much.. I will read that whole post now itself.I asked for a scrap metal and you gave me platinum!! – user2174331 Mar 15 '13 at 14:50

4 Answers4

2

You cannot pass a 2D array without specifying the second dimension, since otherwise, parameter "a" will decay to a pointer, the compiler needs to know how long the second dimension is to calculate the offsets (reason is that 2D array is stored as 1D in memory). Therefore, compiler must know size of *a, which requires that the second dimension be given. You can use vector of vectors to replace 2D array.

taocp
  • 23,276
  • 10
  • 49
  • 62
1

with void function2(a[][4]) it knows that there are 4 elements in each row. With void function3(a[][]) it doesn't know, so it can't calculate where a[i] should be.
Use a vector, since it's c++

spiritwolfform
  • 2,263
  • 15
  • 16
1

C style arrays don't work the way you think. Think of them as a block of memory, and the dimensions tell the compiler how far to offset from the original address.

int a[] is basically a pointer and every element is an int, which means a[1] is equivalent of *(a + 1), where each 1 is sizeof(int) bytes. There's no limit or end (simplistically speaking) of the a array. You could use a[999999] and the compiler won't care.

int a[][4] is similar, but now the compiler knows that each row is 4*sizeof(int). So a[2][1] is *(a + 2*4 + 1)

int a[][] on the other hand, is an incomplete type, so to the compiler, a[2][1] is *(a + 2*?? + 1), and who know what ?? means.

Don't use int **a, that means an array of pointers, which is most likely what you don't want.

As some have said, with STL, use vectors instead. It's pretty safe to use std::vector<std::vector<int> > a. You'll still be able to get a[2][1].

And while you're at it, use references instead, const std::vector<std::vector<int> > &a. That way, you're not copying the whole array with each function call.

Serge Ivanoff
  • 184
  • 1
  • 5
0

how does compiler calculate address of a[x][y]? well simply:

address_of_a+(x*SECOND_SIZE+y)

imagine now that you want

a[2][3]

compiler has to computes:

address_of_a+(2*SECOND_SIZE+3)

if compiler doesn't know SECOND_SIZE how it can compute this? you have to give it to him explicitly. you are using a[2][1], a[100][13] in your code, so compiler has to know how to compute addresses of these objects.

see more here

4pie0
  • 29,204
  • 9
  • 82
  • 118