Passing a 2D array as a pointer to a pointer

Discussion in 'C Programming' started by PeterOut, Aug 27, 2008.

  1. PeterOut

    PeterOut Guest

    Say I have a function like this.

    int Func(float **fppArg);

    and I have a variable defined thus.

    float faa2DArray[3][3];

    How would I pass faa2DArray to Func()?

    Many thanks in advance,
    Peter.
    PeterOut, Aug 27, 2008
    #1
    1. Advertising

  2. PeterOut <> writes:

    > Say I have a function like this.
    >
    > int Func(float **fppArg);
    >
    > and I have a variable defined thus.
    >
    > float faa2DArray[3][3];
    >
    > How would I pass faa2DArray to Func()?


    You can't, at least not directly. You can write:

    float *tmp[] = { faa2DArray[0], faa2DArray[1], faa2DArray[2] };
    Func(tmp);

    or even:

    Func((float *[]){faa2DArray[0], faa2DArray[1], faa2DArray[2]});

    if you don't mind straying into C99. This is more universal:

    float *tmp[3];
    tmp[0] = faa2DArray[0];
    tmp[1] = faa2DArray[1];
    tmp[2] = faa2DArray[2];
    Func(tmp);

    However, the fact that you need these gymnastics suggests that
    something has gone wrong. Can't you start with the right shape of
    array in the first place, or change Func to take the array you have?

    --
    Ben.
    Ben Bacarisse, Aug 28, 2008
    #2
    1. Advertising

  3. PeterOut

    Default User Guest

    PeterOut wrote:

    > Say I have a function like this.
    >
    > int Func(float **fppArg);
    >
    > and I have a variable defined thus.
    >
    > float faa2DArray[3][3];
    >
    > How would I pass faa2DArray to Func()?


    You wouldn't. You have to change the declaration of either fppArg or
    faa2DArray.


    One way would be:

    int Func(float fppArg[][3]);



    Brian
    Default User, Aug 28, 2008
    #3
  4. On 27 Aug, 23:53, PeterOut <> wrote:
    > Say I have a function like this.
    >
    > int Func(float **fppArg);
    >
    > and I have a variable defined thus.
    >
    > float faa2DArray[3][3];
    >
    > How would I pass faa2DArray to Func()?


    http://c-faq.com/

    FAQ 6.18 "My compiler complained when I passed a two-dimensional
    array to a function expecting a pointer to a pointer. "

    then read all of section 6. Then read the rest of the FAQ.

    --
    Nick Keighley

    "Resistance is futile. Read the C-faq."
    -- James Hu (c.l.c.)
    Nick Keighley, Aug 28, 2008
    #4
    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. JW
    Replies:
    1
    Views:
    432
    David Jeske
    Jul 12, 2003
  2. J W
    Replies:
    0
    Views:
    382
  3. entitledX
    Replies:
    4
    Views:
    503
    Old Wolf
    Jan 12, 2006
  4. jimjim
    Replies:
    16
    Views:
    822
    Jordan Abel
    Mar 28, 2006
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    439
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page