Passing a contiguously allocated 2D array to a function expecting a 1 dimesnional array

J

Jonathan Underwood

Hi

I have a 2 dimensional array allocated as a contiguous chunk of
memory, as described in Question 6.16 in the FAQ (i.e. in the manner
of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html).
I have a function declared as func(..., array[],...) and I wish to
pass my 2D array to that function. As I understand it, this should be
allowable since the 2D array is contiguous in memory, and if i
therefore call it as func(..., my2darray[0], ...) the data types
should match (double * in both cases). Is this the correct way to do
this?

Many thanks
Jonathan.
 
T

Tim Prince

Jonathan said:
Hi

I have a 2 dimensional array allocated as a contiguous chunk of
memory, as described in Question 6.16 in the FAQ (i.e. in the manner
of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html).
I have a function declared as func(..., array[],...) and I wish to
pass my 2D array to that function. As I understand it, this should be
allowable since the 2D array is contiguous in memory, and if i
therefore call it as func(..., my2darray[0], ...) the data types
should match (double * in both cases). Is this the correct way to do
this?

Should work, but this makes it look like you are trying to conform to the
f2c version of Fortran 77, rather than programming to the style of any
programming language.
 
S

Simon Biber

Jonathan Underwood said:
I have a 2 dimensional array allocated as a contiguous chunk of
memory, as described in Question 6.16 in the FAQ (i.e. in the manner
of array 2 on the page http://www.eskimo.com/~scs/C-faq/q6.16.html).
I have a function declared as func(..., array[],...) and I wish to
pass my 2D array to that function. As I understand it, this should be
allowable since the 2D array is contiguous in memory, and if i
therefore call it as func(..., my2darray[0], ...) the data types
should match (double * in both cases). Is this the correct way to do
this?

Yes, this is fine with the definition as in FAQ 6.16:
int **array2 = (int **)malloc(nrows * sizeof(int *));
array2[0] = (int *)malloc(nrows * ncolumns * sizeof(int));

However, if it were a normal array with a definition like:
double my2darray[4][7];

The expression my2darray[0] has type 'double[7]' or 'array of 7 double'.
It is undefined behaviour to attempt to access beyond the 7th element
of my2darray[0], that is beyond my2darray[0][6], so you're not supposed
to access the whole of my2darray through my2darray[0].

This could generate problems on a really good bounds-checking
implementation of C. Most implementations won't catch it though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top