Why can't the parameter be const in this program?

Discussion in 'C Programming' started by Eric Lilja, Aug 24, 2005.

  1. Eric Lilja

    Eric Lilja Guest

    Hello! Consider this simple C program:
    #include <stdio.h>

    static void print_cards(const int[][13]);

    int
    main(void)
    {
    int deck[4][13] = {{0}};

    print_cards(deck);

    return 0;
    }

    static void
    print_cards(const int deck[][13])
    {
    int row = 0;
    int column = 0;

    for(row = 0; row < 4; ++row)
    for(column = 0; column < 13; ++column)
    printf("[%i][%i] = %i\n", row, column, deck[row][column]);
    }

    When compiled, my compiler complains:
    gcc -Wall -W -ansi -pedantic -g -c -o pass_2d_array.o pass_2d_array.c
    pass_2d_array.c: In function `main':
    pass_2d_array.c:28: warning: passing arg 1 of `print_cards' from
    incompatible pointer type

    If I remove the const from the type of the single parameter, it
    compiles cleanly. Why can't it be const? It wrote a version that
    operates on the 2d-array using a pointer-to-int and that can, of
    course, be const.

    / Eric
     
    Eric Lilja, Aug 24, 2005
    #1
    1. Advertising

  2. Eric Lilja

    Kuku Guest

    I think you must be getting a warning at

    printf("[%i][%i] = %i\n", row, column, deck[row][column]);

    I believe that since deck is a const, as in the function argument,
    while doing a printf, it uses a variable column which is not a const
    and hence the conflict.

    Eric Lilja wrote:
    > Hello! Consider this simple C program:
    > #include <stdio.h>
    >
    > static void print_cards(const int[][13]);
    >
    > int
    > main(void)
    > {
    > int deck[4][13] = {{0}};
    >
    > print_cards(deck);
    >
    > return 0;
    > }
    >
    > static void
    > print_cards(const int deck[][13])
    > {
    > int row = 0;
    > int column = 0;
    >
    > for(row = 0; row < 4; ++row)
    > for(column = 0; column < 13; ++column)
    > printf("[%i][%i] = %i\n", row, column, deck[row][column]);
    > }
    >
    > When compiled, my compiler complains:
    > gcc -Wall -W -ansi -pedantic -g -c -o pass_2d_array.o pass_2d_array.c
    > pass_2d_array.c: In function `main':
    > pass_2d_array.c:28: warning: passing arg 1 of `print_cards' from
    > incompatible pointer type
    >
    > If I remove the const from the type of the single parameter, it
    > compiles cleanly. Why can't it be const? It wrote a version that
    > operates on the 2d-array using a pointer-to-int and that can, of
    > course, be const.
    >
    > / Eric
     
    Kuku, Aug 24, 2005
    #2
    1. Advertising

  3. Eric Lilja

    Ian Guest

    Eric Lilja wrote:
    > Hello! Consider this simple C program:
    > #include <stdio.h>
    >
    > static void print_cards(const int[][13]);
    >
    > int
    > main(void)
    > {
    > int deck[4][13] = {{0}};
    >
    > print_cards(deck);
    >
    > return 0;
    > }
    >
    > static void
    > print_cards(const int deck[][13])
    > {
    > int row = 0;
    > int column = 0;
    >
    > for(row = 0; row < 4; ++row)
    > for(column = 0; column < 13; ++column)
    > printf("[%i][%i] = %i\n", row, column, deck[row][column]);
    > }
    >
    > When compiled, my compiler complains:
    > gcc -Wall -W -ansi -pedantic -g -c -o pass_2d_array.o pass_2d_array.c
    > pass_2d_array.c: In function `main':
    > pass_2d_array.c:28: warning: passing arg 1 of `print_cards' from
    > incompatible pointer type
    >

    Compiler bug?

    Ian
     
    Ian, Aug 24, 2005
    #3
  4. Eric Lilja <> wrote:

    (I've trimmed your program down to the minimum.)

    > void print_cards(const int deck[][13]) {}


    > int
    > main(void)
    > {
    > int deck[4][13] = {{0}};
    > print_cards(deck);
    > return 0;
    > }


    > If I remove the const from the type of the single parameter, it
    > compiles cleanly. Why can't it be const? It wrote a version that
    > operates on the 2d-array using a pointer-to-int and that can, of
    > course, be const.


    This is a FAQ:

    http://www.eskimo.com/~scs/C-faq/q11.10.html

    There is an excellent thread in Google's archives which explains just
    why what you're trying is not permitted:

    http://snipurl.com/h6oi

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Aug 24, 2005
    #4
  5. Eric Lilja wrote on 24/08/05 :
    > When compiled, my compiler complains:
    > gcc -Wall -W -ansi -pedantic -g -c -o pass_2d_array.o pass_2d_array.c
    > pass_2d_array.c: In function `main':
    > pass_2d_array.c:28: warning: passing arg 1 of `print_cards' from
    > incompatible pointer type
    >
    > If I remove the const from the type of the single parameter, it
    > compiles cleanly. Why can't it be const? It wrote a version that
    > operates on the 2d-array using a pointer-to-int and that can, of
    > course, be const.


    Must be a pointer to array :

    #include <stdio.h>

    static void print_cards (int (* const deck)[4][13])
    {
    int row = 0;

    for (row = 0; row < 4; ++row)
    {
    int column = 0;

    for (column = 0; column < 13; ++column)
    {
    printf ("[%2d][%2d] = %d\n", row, column,
    (*deck)[row][column]);
    }
    }
    }

    int main (void)
    {
    int deck[4][13] =
    {
    {0}
    };

    print_cards (&deck);

    return 0;
    }

    --
    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, Aug 24, 2005
    #5
  6. Eric Lilja

    Netocrat Guest

    On Wed, 24 Aug 2005 18:34:01 -0700, Old Wolf wrote:
    > Christopher Benson-Manica wrote:
    >> Eric Lilja <> wrote:
    >>
    >>> void print_cards(const int deck[][13]) {}

    >>
    >>> int
    >>> main(void)
    >>> {
    >>> int deck[4][13] = {{0}};
    >>> print_cards(deck);
    >>> return 0;
    >>> }

    >>
    >>> If I remove the const from the type of the single parameter, it
    >>> compiles cleanly. Why can't it be const? It wrote a version that
    >>> operates on the 2d-array using a pointer-to-int and that can, of
    >>> course, be const.

    >>
    >> There is an excellent thread in Google's archives which explains just
    >> why what you're trying is not permitted:
    >>
    >> http://snipurl.com/h6oi

    >
    > Actually that thread is discussing why a char ** can't be
    > implicitly converted to a char const **.
    >
    > The issue here is that an array of X can't be implicitly
    > converted to an array of const X.
    >
    > There was a thread on this, that I contributed to, a few months
    > ago, but I don't recall its title.


    That would be the one started by Alexei Frounze, titled from memory
    "gcc: pointer to array", which became a huge and scattered thread that
    deviated greatly from the original topic.

    That thread also referred to the one above; and it seems that even though
    unnecessary, C treats arrays by the same rule as pointers in this case so
    http://snipurl.com/h6oi is probably a reasonable place to look after all.

    --
    http://members.dodo.com.au/~netocrat
     
    Netocrat, Aug 25, 2005
    #6
  7. Eric Lilja

    Old Wolf Guest

    Christopher Benson-Manica wrote:
    > Eric Lilja <> wrote:
    >
    >> void print_cards(const int deck[][13]) {}

    >
    >> int
    >> main(void)
    >> {
    >> int deck[4][13] = {{0}};
    >> print_cards(deck);
    >> return 0;
    >> }

    >
    >> If I remove the const from the type of the single parameter, it
    >> compiles cleanly. Why can't it be const? It wrote a version that
    >> operates on the 2d-array using a pointer-to-int and that can, of
    >> course, be const.

    >
    > There is an excellent thread in Google's archives which explains just
    > why what you're trying is not permitted:
    >
    > http://snipurl.com/h6oi


    Actually that thread is discussing why a char ** can't be
    implicitly converted to a char const **.

    The issue here is that an array of X can't be implicitly
    converted to an array of const X.

    There was a thread on this, that I contributed to, a few months
    ago, but I don't recall its title.
     
    Old Wolf, Aug 25, 2005
    #7
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,216
    Smokey Grindel
    Dec 2, 2006
  2. Replies:
    11
    Views:
    1,151
  3. Javier
    Replies:
    2
    Views:
    620
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,171
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    944
    fungus
    Oct 31, 2008
Loading...

Share This Page