multi dimension array initialization

Discussion in 'C Programming' started by natkw1@yahoo.com.au, Sep 17, 2005.

  1. Guest

    Hi,
    I'm new to C so hopefully someone can give me some guidance on where
    I've gone wrong.

    I've written the following code, trying to initialize the multi-dim
    array but it's not working:

    #include <stdio.h>
    #include <ctype.h>

    #define ROW 2
    #define COL 5

    void init_array();

    main ()
    {
    char array[ROW][COL];

    init_array(array);
    return 0;
    }

    void init_array(char data[][])
    {
    int r, c;

    for (r=0; r<=ROW; r++)
    {
    for (c=0; c<=COL; c++)
    {
    data[r][c] = NULL;
    }
    }
    }

    But when I try to compile with gcc, I'm getting this message
    a2.c: In function `init_array':
    a2.c:28: arithmetic on pointer to an incomplete type

    Can anyone explain what the error means in english ?
    What am I doing wrong ?

    Nat

    I've tried posting to comp.lang.c.moderated but I must have done
    something incorrect as it hasn't been posted yet. My appologies if it
    appears twice.
     
    , Sep 17, 2005
    #1
    1. Advertising

  2. wrote on 17/09/05 :
    > #include <stdio.h>
    > #include <ctype.h>
    >
    > #define ROW 2
    > #define COL 5
    >
    > void init_array();


    Not-a-prototype (IOW, useless declaration.)

    > main ()
    > {
    > char array[ROW][COL];
    >
    > init_array(array);
    > return 0;
    > }
    >
    > void init_array(char data[][])


    Dimensions required (the leftmost can be ommited). Better to pass the
    dimensions too. (Or use a pointer to array).

    > {
    > int r, c;
    >
    > for (r=0; r<=ROW; r++)


    You are going ot of the limits by 1.

    > {
    > for (c=0; c<=COL; c++)


    ditto.

    > {
    > data[r][c] = NULL;


    NULL is for pointers. Use 0 for an int.

    > }
    > }
    > }


    Try that. Ask for details you don't understand.

    #include <stdio.h>
    #include <ctype.h>

    #define ROW 2
    #define COL 5

    static void init_array (char data[][COL], size_t row, size_t col)
    {
    size_t r;

    for (r = 0; r < row; r++)
    {
    size_t c;
    for (c = 0; c < col; c++)
    {
    data[r][c] = r + c;
    }
    }
    }

    static void print_array (char const data[][COL], size_t row, size_t
    col)
    {
    size_t r;

    for (r = 0; r < row; r++)
    {
    size_t c;
    for (c = 0; c < col; c++)
    {
    printf ("%3d", data[r][c]);
    }
    printf ("\n");
    }
    }

    int main (void)
    {
    char array[ROW][COL];

    init_array (array, ROW, COL);
    print_array (array, ROW, COL);
    return 0;
    }

    0 1 2 3 4
    1 2 3 4 5

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    I once asked an expert COBOL programmer, how to
    declare local variables in COBOL, the reply was:
    "what is a local variable?"
     
    Emmanuel Delahaye, Sep 17, 2005
    #2
    1. Advertising

  3. pete Guest

    wrote:
    >
    > Hi,
    > I'm new to C so hopefully someone can give me some guidance on where
    > I've gone wrong.
    >
    > I've written the following code, trying to initialize the multi-dim
    > array but it's not working:
    >
    > #include <stdio.h>
    > #include <ctype.h>
    >
    > #define ROW 2
    > #define COL 5
    >
    > void init_array();
    >
    > main ()
    > {
    > char array[ROW][COL];
    >
    > init_array(array);
    > return 0;
    > }
    >
    > void init_array(char data[][])
    > {
    > int r, c;
    >
    > for (r=0; r<=ROW; r++)
    > {
    > for (c=0; c<=COL; c++)
    > {
    > data[r][c] = NULL;
    > }
    > }
    > }
    >
    > But when I try to compile with gcc, I'm getting this message
    > a2.c: In function `init_array':
    > a2.c:28: arithmetic on pointer to an incomplete type
    >
    > Can anyone explain what the error means in english ?


    I wish I could.
    I'm not good at explantions.

    /* BEGIN new.c */

    #include <stdio.h>

    #define ROW 2
    #define COL 5

    void init_array(char (*data)[5]);
    void print_array(char (*data)[5]);

    int main (void)
    {
    char array[ROW][COL];

    init_array(array);
    print_array(array);
    return 0;
    }

    void init_array(char (*data)[5])
    {
    int r, c;

    for (r = 0; r != ROW; ++r) {
    for (c = 0; c != COL; ++c) {
    data[r][c] = (char)(r + c);
    }
    }
    }

    void print_array(char (*data)[5])
    {
    int r, c;

    for (r = 0; r != ROW; ++r) {
    for (c = 0; c != COL; ++c) {
    printf("%d ", data[r][c]);
    }
    }
    putchar('\n');
    }

    /* END new.c */


    --
    pete
     
    pete, Sep 17, 2005
    #3
  4. wrote:
    > Hi,
    >
    > #include <stdio.h>
    > #include <ctype.h>

    You're not using anything from ctype.h, then why include it?

    >
    > #define ROW 2
    > #define COL 5
    >
    > void init_array();

    When you declare a prototype, make sure you declare the parameters too,
    improves readability. And if I'm not wrong, then a C compiler assumes
    integer parameters when none are specified in the prototype.

    >
    > main ()

    It should be "int main(void)"

    > {
    > char array[ROW][COL];
    >
    > init_array(array);
    > return 0;
    > }
    >
    > void init_array(char data[][])

    For unsized arrays, you can leave out only the leftmost dimension.
    You must specify the rest. So correct the above as:
    "void init_array(char data[][COL])"

    > {
    > int r, c;
    >
    > for (r=0; r<=ROW; r++)
    > {
    > for (c=0; c<=COL; c++)
    > {
    > data[r][c] = NULL;

    Use "data[r][c] = 0;". NULL is defined as a pointer to nothing, the
    above statement will cause this warning: "assignment makes integer from
    pointer without a cast"

    > }
    > }
    > }
    >
    > But when I try to compile with gcc, I'm getting this message
    > a2.c: In function `init_array':
    > a2.c:28: arithmetic on pointer to an incomplete type
    >
    > Can anyone explain what the error means in english ?
    > What am I doing wrong ?
    >
    > Nat
    >
    > I've tried posting to comp.lang.c.moderated but I must have done
    > something incorrect as it hasn't been posted yet. My appologies if it
    > appears twice.
    >


    cheers,
    forayer

    --
    If you would be a real seeker after truth, it is necessary that at least
    once in your life you doubt, as far as possible, all things."
    -- Rene Descartes
     
    Vimal Aravindashan, Sep 17, 2005
    #4
  5. Vimal Aravindashan wrote on 17/09/05 :
    >> void init_array();

    > When you declare a prototype,


    It's not a prototype. It's a declaration.

    > make sure you declare the parameters too,
    > improves readability.


    and it makes it a separated prototype.

    > And if I'm not wrong, then a C compiler assumes integer
    > parameters when none are specified in the prototype.


    Nope. As a function declaration, it assumes that the parameters are
    unspecified.

    OTOH,

    int f()
    {
    ...
    }

    is a function with no parameters. Similar to

    int f(void)
    {
    ...
    }

    except that the first form has not a prototype, but the second has one.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    ..sig under repair
     
    Emmanuel Delahaye, Sep 17, 2005
    #5
  6. Emmanuel Delahaye wrote:
    > Vimal Aravindashan wrote on 17/09/05 :
    >
    >>> void init_array();

    >>
    >> When you declare a prototype,

    >
    >
    > It's not a prototype. It's a declaration.

    Oops my mistake, careless choice of words, sorry. :)
    I meant "When you declare a funtion, ..."

    >
    >> make sure you declare the parameters too, improves readability.

    >
    >
    > and it makes it a separated prototype.
    >
    >> And if I'm not wrong, then a C compiler assumes integer parameters
    >> when none are specified in the prototype.

    >
    >
    > Nope. As a function declaration, it assumes that the parameters are
    > unspecified.

    Yes, but in case of implicit declarations it does assume integer return
    type and integer parameters.

    >
    > OTOH,
    >
    > int f()
    > {
    > ...
    > }
    >
    > is a function with no parameters. Similar to
    >
    > int f(void)
    > {
    > ...
    > }
    >
    > except that the first form has not a prototype, but the second has one.
    >



    --
    If you would be a real seeker after truth, it is necessary that at least
    once in your life you doubt, as far as possible, all things."
    -- Rene Descartes
     
    Vimal Aravindashan, Sep 17, 2005
    #6
  7. Vimal Aravindashan <> writes:
    > wrote:

    [...]
    >> {
    >> int r, c;
    >> for (r=0; r<=ROW; r++)
    >> {
    >> for (c=0; c<=COL; c++)
    >> {
    >> data[r][c] = NULL;

    > Use "data[r][c] = 0;". NULL is defined as a pointer to nothing, the
    > above statement will cause this warning: "assignment makes integer
    > from pointer without a cast"


    Not necessarily. An unadorned 0 is a valid null pointer constant, and
    therefore a valid expansion for the NULL macro. You certainly
    shouldn't assign NULL to an int object, but it's
    implementation-defined whether the compiler will warn you about it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 17, 2005
    #7
  8. Guest

    Thank you all for your advice. It's been very helpful.

    I messed up my prototyping ( book said you can just leave it blank )
    but following your advice:
    my prototype is void init_array(char data[][COL]);
    and it's function init_array(char data[][COL]);
    and changed the loop condition so it doesn't go over by one.
    The rest of the code is left as is and it works !!!
     
    , Sep 17, 2005
    #8
  9. writes:
    > Thank you all for your advice. It's been very helpful.
    >
    > I messed up my prototyping ( book said you can just leave it blank )
    > but following your advice:
    > my prototype is void init_array(char data[][COL]);
    > and it's function init_array(char data[][COL]);
    > and changed the loop condition so it doesn't go over by one.
    > The rest of the code is left as is and it works !!!


    Get a better book. K&R2 (Kernighan & Ritchie, "The C Programming
    Language", 2nd edition) is a good one.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 18, 2005
    #9
  10. Flash Gordon Guest

    Vimal Aravindashan wrote:
    > Emmanuel Delahaye wrote:
    >
    >> Vimal Aravindashan wrote on 17/09/05 :
    >>
    >>>> void init_array();
    >>>
    >>> When you declare a prototype,

    >>
    >> It's not a prototype. It's a declaration.

    >
    > Oops my mistake, careless choice of words, sorry. :)
    > I meant "When you declare a funtion, ..."
    >
    >>> make sure you declare the parameters too, improves readability.

    >>
    >> and it makes it a separated prototype.
    >>
    >>> And if I'm not wrong, then a C compiler assumes integer parameters
    >>> when none are specified in the prototype.

    >>
    >> Nope. As a function declaration, it assumes that the parameters are
    >> unspecified.

    >
    > Yes, but in case of implicit declarations it does assume integer return
    > type and integer parameters.


    No, you get implicit int for the return type in C89 if you don't specify
    one, but for the arguments the default promotions will apply. So if you
    pass a char it will on most systems be promoted to an int, but if you
    pass a pointer will be passed as the pointer type you passed.

    <snip>
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Sep 18, 2005
    #10
  11. Randy Howard Guest

    wrote
    (in article
    <>):

    > I messed up my prototyping ( book said you can just leave it blank )


    What book? The author wouldn't happen to be "Schildt" by any
    chance? If so, set fire to it, erase from your memory everthing
    contained within, and start over with a new book.

    --
    Randy Howard (2reply remove FOOBAR)
     
    Randy Howard, Sep 29, 2005
    #11
  12. "Randy Howard" <> wrote in message
    news:...
    > What book? The author wouldn't happen to be "Schildt" by any
    > chance? If so, set fire to it, erase from your memory everthing
    > contained within, and start over with a new book.


    And never ever by any books by him but perhaps fairy tales :) Recommend his
    books only to your enemies if you've got any :)

    Alex
     
    Alexei A. Frounze, Sep 29, 2005
    #12
    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. Adam Hartshorne

    Multi-Dimension Array Question

    Adam Hartshorne, Jun 8, 2005, in forum: C++
    Replies:
    6
    Views:
    2,149
  2. Makiyo
    Replies:
    3
    Views:
    537
    Richard Heathfield
    Feb 22, 2004
  3. Eric Laberge

    Multi-dimension array assignments

    Eric Laberge, Aug 22, 2005, in forum: C Programming
    Replies:
    3
    Views:
    464
    Dave Thompson
    Aug 27, 2005
  4. Replies:
    12
    Views:
    591
    Thad Smith
    Oct 10, 2005
  5. Luuk
    Replies:
    15
    Views:
    836
    Nobody
    Feb 11, 2010
Loading...

Share This Page