0

I thought of doing

int arr[row][col];

But I guess since I am to pass the entire arr to a function multiple number of times, hence it may give me stackoverflow [since ROW and COL can be a few thousands]. Hence if I do it using pointers instead then passing on the pointer would be a better way , since I also intend to change the values of the array as it passes through various functions.

How do I define the array using pointer and how do I pass it to the function? Intend to do arr[i][j] whenever I want to access an element.

BenMorel
  • 30,280
  • 40
  • 163
  • 285
Kraken
  • 20,468
  • 32
  • 90
  • 145
  • possible duplicate of [passing 2D array to function](http://stackoverflow.com/questions/8767166/passing-2d-array-to-function) –  Jul 12 '13 at 12:03

2 Answers2

3

When you pass arrays around as arguments, you only pass a pointer to its first element, not the entire array.

So the function signature could look something like this:

void some_function(int arr[][col]);

Or optionally

void some_function(int (*arr)[col]);

If the column size is not a global compile-time constant, then you can pass it as argument to the function as well:

void some_function(const size_t col, int arr[][col]);

Or

void some_function(const size_t col, int (*arr)[col]);
Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
  • This will just put the arr pointer on the stack with the message call and not the entire array? – Kraken Jul 12 '13 at 09:07
  • OP wants to change values of array through various functions too.So i guess it should be pointer to pointer rather than just pointer. – Dayal rai Jul 12 '13 at 09:07
  • @Kraken That's right, arrays decays to pointers when passed around, so only pointers will be passed not the complete (or parts of the ) array. – Some programmer dude Jul 12 '13 at 09:09
  • @Dayalrai A pointer to pointer and an array or arrays (or a pointer to an array) are two very different things and are not compatible. See e.g. [this answer of mine](http://stackoverflow.com/a/17566806/440558) for why. – Some programmer dude Jul 12 '13 at 09:09
  • @JoachimPileborg If I do this `void hello(int arr[ROW][COLUMN])` and to this array I pass `hello(arr)` where arr is an array of size 10000*10000. Still just the pointer is copied? – Kraken Jul 12 '13 at 09:10
  • @Kraken It will put a pointer of type `int(*arr)[col]` on the stack – Suvarna Pattayil Jul 12 '13 at 09:10
  • @JoachimPileborg Then I have a problem here http://stackoverflow.com/questions/16296138/while-making-a-function-call-in-c-is-the-stack-of-os-used-and-is-the-size-of-s . Why do I get a segfault in this case if just a pointer is passed? The pointer wont take more than a 4 bytes I guess. Also, if I change the size to 100*100 it works fine – Kraken Jul 12 '13 at 09:12
  • @Kraken Because of the local array in `main`. It's 400000000 bytes, almost 400 times bigger than the default stack. If you make the variable global or `static` it will no longer be on the stack. – Some programmer dude Jul 12 '13 at 09:14
  • @JoachimPileborg Ohh so the problem is in main declaration that the stack can not hold so much data. And not with the passing. Thanks. Great help. – Kraken Jul 12 '13 at 09:15
  • @JoachimPileborg One last thing, is there any difference in the way that you are passing and the way I am passing? – Kraken Jul 12 '13 at 09:16
  • @Kraken No not really. The dimension of the outermost array is not used though, which is why I made it empty. – Some programmer dude Jul 12 '13 at 09:21
0

Passing the array to a function is OK. Arrays are treated like pointers when passing as argument, that is, only the address of the first element is copied.

But be sure to pass number of rows and columns as arguments too.

Manas
  • 588
  • 2
  • 14