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

Discussion in 'C Programming' started by Jonathan Underwood, Aug 12, 2003.

  1. 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.
     
    Jonathan Underwood, Aug 12, 2003
    #1
    1. Advertising

  2. Jonathan Underwood

    Tim Prince Guest

    Jonathan Underwood wrote:

    > 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.
    --
    Tim Prince
     
    Tim Prince, Aug 13, 2003
    #2
    1. Advertising

  3. Jonathan Underwood

    Simon Biber Guest

    "Jonathan Underwood" <> wrote:
    > 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.

    --
    Simon.
     
    Simon Biber, Aug 13, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    622
    Matt Wharton
    Dec 9, 2004
  2. MackS
    Replies:
    11
    Views:
    645
    Richard Bos
    Jan 31, 2005
  3. alamaison
    Replies:
    20
    Views:
    1,045
    peter koch
    Jan 15, 2009
  4. Replies:
    4
    Views:
    678
  5. Mister B
    Replies:
    8
    Views:
    554
    Nick Keighley
    Aug 26, 2010
Loading...

Share This Page