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

E

Eric Lilja

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
 
K

Kuku

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.
 
I

Ian

Eric said:
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
 
C

Christopher Benson-Manica

(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
 
E

Emmanuel Delahaye

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?"
 
N

Netocrat

Christopher said:
Eric Lilja said:
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.
 
O

Old Wolf

Christopher said:
Eric Lilja said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top