Passing 2D arrays to functions with const. 
Author Message
 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
--



Sun, 27 Mar 2005 06:28:43 GMT  
 Passing 2D arrays to functions with const.

Quote:
>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 */

>}

>int main() {

>  int deck[4][13] = { 0 };

>  deal(deck);

>}
>t.c: In function `main':
>t.c:12: warning: passing arg 1 of `deal' from incompatible pointer type

Is there some reason you didn't like the answers you received in
comp.lang.c?  Do you think they will be any different here?

<<Remove the del for email>>
--



Mon, 28 Mar 2005 22:09:08 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Passing 2D arrays to functions with const.

2. Passing 2D arrays to function

3. Passing 2D Arrays to functions

4. pass 2d array to function ?

5. How to pass 2d array of structure to a function

6. Passing 2D array as function arg.

7. passing 2D arrays to functions

8. Passing 2D arrays to functions

9. Passing 2D dynamic array to function

10. How To pass 2D String array to VB from VC++ Using Safe array

11. Which 2D array of 2D array addressing method?

12. 2D array of pointers to 2D arrays

 

 
Powered by phpBB® Forum Software