Thread: Pointer(s) Error

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    68

    Pointer(s) Error

    Im reading C: How to Program and I have got up to pointers, one of the examples shown goes with a deck of cards and shuffles then displays the 52 cards.

    I am having an error in the main function with the pointers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle(int [][13]);
    void deal(const int [][13], const char *[], const char *[]);
    
    main()
    {
    
    	char *suit[4] = {"Hearts","Diamonds","Clubs","Spades"};
    	char *face[13] = {"Ace","Deuce","Three","Four",
    			"Five","Six","Seven","Eight",
    			"Nine","Ten","Jack","Queen","King"};
    			
    	int deck[4][13] = {0};
    	
    	srand(time(NULL));
    	
    	shuffle(deck);
    	deal(deck,face,suit);
    	
    	return 0;
    
    }
    
    void shuffle(int wDeck[][13])
    {
    
    	int card, row, column;
    	
    	for (card=1;card<=52;card++) {
    	
    		row = rand()%4;
    		column = rand()%13;
    		
    		while (wDeck[row][column] != 0) {
    		
    			row = rand()%4;
    			column = rand()%13;
    		
    		}
    		
    		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');
    
    }
    Is the code I am currently using, the error message is this

    deck.c: In function `main':
    deck.c:21: warning: passing arg 1 of `deal' from incompatible pointer type
    deck.c:21: warning: passing arg 2 of `deal' from incompatible pointer type
    deck.c:21: warning: passing arg 3 of `deal' from incompatible pointer type
    If its too any help I am using gcc

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    well i got it to compile by doing this
    Code:
    void deal(const int [][13], const char *, const char *); // prototype
    
    ...
    ...
         //   deal(deck,face,suit); <- change this
    
    deal(deck,face[0],suit[0]); // <- to this
    
    void deal(const int wDeck[][13], const char *wFace, const char *wSuit) // <- function parameters
    But i got a seg fault

    your seg fault is here
    Code:
    printf("%5s of %-8s%c",wFace[column], wSuit[row],card % 2 == 0? '\n' : '\t');
    i say forget pointers for now
    go back to your book and read about how to use printf properly
    Last edited by sand_man; 11-25-2004 at 04:29 AM.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    First of all, this is not a good example to start learning pointers. If that's what you're doing, try to start with simple non-bloated code. Especially with code that doesn't throw warnings

    Next, it's not an error but just a (mild?) warning. The problem is that deal expects its parameters to be pointers to constant values while they are not declared const in main. If you would declare them const, shuffle wouldn't work anymore, because it actually does modify those values.
    A possible solution is this code, but it's not quite so easy to understand because of the use of arrays. Again, really bad example.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void shuffle(int [][13]);
    void deal(int (*const wDeck)[13],  char *const wFace[], char *const wSuit[]);
    
    main()
    {
    
    	char *suit[4] = {"Hearts","Diamonds","Clubs","Spades"};
    	char *face[13] = {"Ace","Deuce","Three","Four",
    			"Five","Six","Seven","Eight",
    			"Nine","Ten","Jack","Queen","King"};
    			
    	int deck[4][13] = {0};
    	
    	srand(time(NULL));
    	
    	shuffle(deck);
    	deal(deck,face,suit);
    	
    	return 0;
    
    }
    
    void shuffle(int wDeck[][13])
    {
    
    	int card, row, column;
    	
    	for (card=1;card<=52;card++) {
    	
    		row = rand()%4;
    		column = rand()%13;
    		
    		while (wDeck[row][column] != 0) {
    		
    			row = rand()%4;
    			column = rand()%13;
    		
    		}
    		
    		wDeck[row][column]=card;
    	
    	}
    
    }
    
    void deal(int (*const wDeck)[13],  char *const wFace[], char *const 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');
    
    }
    I think the author confuses a pointer to a constant with a constant pointer, i.e. char * const * is a non-constant pointer to a constant pointer to non-constant chars.

    edit: Personally, I keep a good distance to code that mixes pointers and arrays. It's just too error-prone (for me). So if my code isn't correct either (shouldn't throw any warnings though), some guru please correct me
    Last edited by Nyda; 11-25-2004 at 04:43 AM.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    That seemed to have worked Nyda thanks. sand_man, my book doesnt go deeply into prinft until the standard library is talked about however it doesnt explain much with what arguments and %f type things are allowed. Any good tutorial just on printf that you know of?

    Also this was actually the last example on pointers, the rest worked pretty well.
    Last edited by gsoft; 11-25-2004 at 05:43 AM.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by sand_man
    well i got it to compile by doing this
    Code:
    void deal(const int [][13], const char *, const char *);
    ...
    ...

    i say forget pointers for now
    go back to your book and read about how to use printf properly
    The example uses printf properly. It breaks because you turn an array of pointers to chars into a pointer to a string.
    i.e. when deal() accesses wFace[column] it now believes wFace is a pointer to a string (to "ace") and that you are trying to access character column within that string at *(wFace +column). As soon as a column > 4 is accessed [strlen("ace")+1 ('\0')] you might get a segfault (if no valid data is at that location in memory).
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM