2D array, compiler error

Discussion in 'C Programming' started by Imran, Aug 30, 2007.

  1. Imran

    Imran Guest

    Hello all, I am trying to pass 2D array to a func, as follows, it is showing
    compiler error.

    void helloptr(int **a){

    a[0][0] = 0xB;

    }

    int main(int argc, char* argv[]){

    int a[10][10]={0};

    helloptr(a);

    return 0;

    }
     
    Imran, Aug 30, 2007
    #1
    1. Advertising

  2. Imran

    Ben Pfaff Guest

    "Imran" <> writes:

    > Hello all, I am trying to pass 2D array to a func, as follows, it is showing
    > compiler error.
    >
    > void helloptr(int **a){
    >
    > a[0][0] = 0xB;
    >
    > }
    >
    > int main(int argc, char* argv[]){
    >
    > int a[10][10]={0};
    >
    > helloptr(a);
    >
    > return 0;
    >
    > }


    This is in the FAQ.

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

    A: The rule (see question 6.3) by which arrays decay into pointers
    is not applied recursively. An array of arrays (i.e. a two-
    dimensional array in C) decays into a pointer to an array, not a
    pointer to a pointer. Pointers to arrays can be confusing, and
    must be treated carefully; see also question 6.13.

    If you are passing a two-dimensional array to a function:

    int array[NROWS][NCOLUMNS];
    f(array);

    the function's declaration must match:

    void f(int a[][NCOLUMNS])
    { ... }

    or

    void f(int (*ap)[NCOLUMNS]) /* ap is a pointer to an array */
    { ... }

    In the first declaration, the compiler performs the usual
    implicit parameter rewriting of "array of array" to "pointer to
    array" (see questions 6.3 and 6.4); in the second form the
    pointer declaration is explicit. Since the called function does
    not allocate space for the array, it does not need to know the
    overall size, so the number of rows, NROWS, can be omitted. The
    width of the array is still important, so the column dimension
    NCOLUMNS (and, for three- or more dimensional arrays, the
    intervening ones) must be retained.

    If a function is already declared as accepting a pointer to a
    pointer, it is almost certainly meaningless to pass a two-
    dimensional array directly to it.

    See also questions 6.12 and 6.15.

    References: K&R1 Sec. 5.10 p. 110; K&R2 Sec. 5.9 p. 113; H&S
    Sec. 5.4.3 p. 126.
    --
    "I don't have C&V for that handy, but I've got Dan Pop."
    --E. Gibbons
     
    Ben Pfaff, Aug 30, 2007
    #2
    1. Advertising

  3. Imran

    CBFalconer Guest

    Imran wrote:
    >
    > Hello all, I am trying to pass 2D array to a func, as follows, it
    > is showing compiler error.
    >
    > void helloptr(int **a) {
    >
    > a[0][0] = 0xB;
    > }


    What 2d array? All I see is a pointer to a pointer to int.
    Sometimes this may be used to reference a 1d array of pointers to
    ints. Read the faq.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Aug 30, 2007
    #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. Yan
    Replies:
    0
    Views:
    1,138
  2. Jack Wright
    Replies:
    5
    Views:
    627
    Shiv Kumar
    Jan 19, 2004
  3. Ram
    Replies:
    0
    Views:
    2,846
  4. Andrey Batyuck

    Compiler compiler with C++ as output

    Andrey Batyuck, May 11, 2004, in forum: C++
    Replies:
    3
    Views:
    440
    Frederik Hertzum
    May 17, 2004
  5. RickMuller
    Replies:
    4
    Views:
    708
    Alexey Shamrin
    Mar 26, 2005
Loading...

Share This Page