Passing incompatible pointer type?

This is a discussion on Passing incompatible pointer type? within the C Programming forums, part of the General Programming Boards category; This is example code straight out of the book, and I have no idea what the problem with it is. ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Passing incompatible pointer type?

    This is example code straight out of the book, and I have no idea what the problem with it is.


    fig07_24.c: In function ‘main’:
    fig07_24.c:21: warning: passing argument 1 of ‘deal’ from incompatible pointer type
    fig07_24.c: At top level:
    fig07_24.c:44: error: conflicting types for ‘deal’
    fig07_24.c:6: error: previous declaration of ‘deal’ was here


    Does anyone have a clue please?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle( int[][ 13 ] );
    void deal( const int[][ 13 ], const char *[], const char *[] );
    
    int main()
    {
       const char *suit[ 4 ] = 
          { "Hearts", "Diamonds", "Clubs", "Spades" };
       const char *face[ 13 ] = 
          { "Ace", "Deuce", "Three", "Four", 
            "Five", "Six", "Seven", "Eight", 
            "Nine", "Ten", "Jack", "Queen", "King" };
       int deck[ 4 ][ 13 ] = { 0 };
    
       srand ( time( 0 ) );
    
       shuffle( deck );
       deal( deck, face, suit );
    
       return 0;
    }
    
    
    void shuffle( int wDeck[][ 13 ] )
    {
       int row, column, card;
    
       for (card = 1; card <= 52; card++) {
          do {
             row = rand() % 4;
             column = rand() % 13;
          } while( wDeck[ row ][ column ] != 0 );
    
          wDeck[ row ][ column ] = card;
       }
    }
    
    
    void deal( const int wDeck[][ 13 ], const char *wFace[], const char 
               wSuit[] )
    {
       int card, row, column;
    
       for (card = 1; card <= 52; card++)
    
          for (row = 0; row <= 3; row++)
    
             for (column = 0; column <= 12; column++)
    
    	    if ( wDeck[ row ][ column ] == card )
    	       printf(" %5s of %-8s%c",
                   wFace[ column ], wSuit[ row ],
                   card % 2 == 0 ? '\n' : '\t' );
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    void deal( const int[][ 13 ], const char *[], const char *[] );
    void deal( const int wDeck[][ 13 ], const char *wFace[], const char 
               wSuit[] )

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    it seems your parameters dont match up:
    Code:
    void deal( const int[][ 13 ], const char *[], const char *[] );
    // ...
    void deal( const int wDeck[][ 13 ], const char *wFace[], const char wSuit[] )
    {
    //...
    when you implement the function deal, the 3rd parameter should be *[] not just [].


    for the error about parameter 1 of deal, it might be because deck is int[][] but your function accepts const int[][]. i thought you could just put 'const <datatype>' to treat it as const, but maybe something else is going on when your trying to do that on a 2-d array.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    182
    Adding the * did solve part of the problem, but I'm still getting

    "
    fig07_24.c: In function ‘main’:
    fig07_24.c:21: warning: passing argument 1 of ‘deal’ from incompatible pointer type
    "

    I took out the const in front of int wDeck[][ 13 ] and the associated prototype, and I was able to compile the program without a problem. But I thought a function could use a non constant variable as a constant. Am I using the wrong version of C or something?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by yougene View Post
    I took out the const in front of int wDeck[][ 13 ] and the associated prototype, and I was able to compile the program without a problem. But I thought a function could use a non constant variable as a constant. Am I using the wrong version of C or something?
    You should be able to, but then again, I don't see the point in doing so since you're passing by value and not by pointer.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    change your function prototype ( i mean remove the const)
    Code:
    void deal( int[][ 13 ], const char *[], const char *[] );
    and in the function definition (remove const)

    Code:
    void deal( int wDeck[][ 13 ], const char *wFace[], const char *wSuit[] )
    ssharish

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    182
    >>Elysia
    I'm on the section where I'm learning about the principle of least privilege and pointers.

    >>ssharish2005
    That fix works ssharish, but was what I did wrong, or is this just a cryptic oddity?

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    182
    It was late last night and I didn't notice the program compiles with the warning just fine. My mistake!

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    I'd just like to say that it's refreshing to see lots of things done well in posted code, such as const-correctness, calling srand only once, int main, properly formatted and easy to read etc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    182
    Thank you, although it's code from the book. I do try to stick to the books coding style as much as possible.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Passing a function pointer to a templated type
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2004, 08:31 PM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 05:17 AM
  5. Passing pointer of class type
    By JaWiB in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2003, 10:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21