Passing 2D array to function

19 Feb 2015

I have several 2D arrays where the "horizontal" lengths vary - for example:

char a[10][20]; char b[10][30]; char c[10][40];

I need to be able to call a function and pass the entire horizontal array from any of the 2d arrays. for example:

send(a[8]); send(b[6]); send(c[5]);

The function send() then needs to do a loop and process each of the 20, 30 or 40 chars that were passed to it. My data seems to be getting garbled when I try this.

Sorry if this doesn't make sense. Thanks for any help.

19 Feb 2015

You need to also provide a length.

In c an array is simply a pointer to a block of memory, there is no run time data structure indicating how that memory is organised.

All your send function gets passed to it is in effect a char *, it has no possible way of knowing how much data to process, all it knows is where the data starts.

Also there is no real distinction between 1d and 2d arrays so it may be an idea to use send(a[8][]) rather than send(a[8]) in order to make it explicit that you are accessing a as a 2D array.

20 Feb 2015

Look at http://www.cplusplus.com/doc/tutorial/arrays/ for a nice explanation .