
Passing 2D arrays to functions with const.
Hi I can't figure out what is wrong with this code:
void deal(const int deck[][13]) {
/* the goal is that deal() can read but not modify the array
deck */
Quote:
}
int main() {
int deck[4][13] = { 0 };
deal(deck);
Quote:
}
t.c: In function `main':
t.c:12: warning: passing arg 1 of `deal' from incompatible pointer type
I've trimmed it down to the basics. The function deal() is declared
to take a const 2D array. My intention was to prevent deal() from
changing the array. There should be no requirement that the array
passed be const itself.
If I change the declaration of deck to:
const int deck[4][13];
the warning goes away, but this is not what I want to do. I want to be able
to change deck outside of the function but not in the function deal().
How is this different from the below which compiles without warning?
void foo(const char a[]) {
Quote:
}
int main() {
char a[10];
foo(a);
Quote:
}
It has been suggested I can get around this problem with the following
cast:
deal((const char (*)[13])deck);
and this looks to work, but I am not fond of it.
Any comments appreciated.
--
---------------------------------------------------------------------------
Part-time instructor, CSCI 2132
Faculty of Computer Science
Dalhousie University
--