0

Say I have array a[][]. I find it is even illegal to claim int a[][] as a parameter in function. What should I do? I could only use int **?

ALso why int a[] is legal as a parameter, simply because, it is essentially a int*?

user3495562
  • 335
  • 1
  • 4
  • 16
  • 1
    This has been [asked already](http://stackoverflow.com/q/9446707/478288). – chrisaycock Jun 29 '14 at 22:03
  • If I do not know the length of array yet, what should I do, thanks ! – user3495562 Jun 29 '14 at 22:05
  • @user3495562 if your C implementation support VLAs (most do) you can pass the stride of **all** inferior dimensions as function parameters. I.e. `void foo(size_t cols, int arr2D[][cols])` for two dimensions, `void bar(size_t rows, size_t cols, arr3D[][rows][cols])` for three dimensions, etc. But `arr[][]` isn't legal C, as a parameter or otherwise. – WhozCraig Jun 29 '14 at 22:08
  • If you can tell us the full problem then we can provide a better solution – Ayush Jun 29 '14 at 22:08
  • You can not be treated as a two-dimensional array or more If you do not know the length of the array other than the top(most left). – BLUEPIXY Jun 29 '14 at 22:08

1 Answers1

1

You should specify the size of 2nd parameter

a[][size]

When you pass an n-dimensional array to a function, then size of last n-1 dimensions must be specified. Only size of 1st dimension can be left blank.

int foo(int a[][size1][size2][size3][size4][size5])
Ayush
  • 2,608
  • 2
  • 22
  • 31
  • Hey, I only the maximum length may be 10000 say, then if when I see the real length is in fact 100, I do not want to waster my memory, what should I do? – user3495562 Jun 29 '14 at 22:14
  • why don't you declare size as global? – Ayush Jun 29 '14 at 22:21
  • I mean I will, but the size I know after reading the file. If I set the global too big, whether it will waste memory? – user3495562 Jun 29 '14 at 22:25
  • Take a global variable say int n; then do foo(a[][n]) and when you read size from file then change the value of n before passing it in function or use pointers and realloc – Ayush Jun 29 '14 at 22:28
  • Thanks, I have many files to read and use the function. I will resort to dymamic allocation. thanks for your help! – user3495562 Jun 29 '14 at 22:35