Thread: Having trouble returning pointer to div_t

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    33

    Having trouble returning pointer to div_t

    I am getting this error when compiling- Operands of '=' have incompatible types 'div_t [5]' and 'div_t *
    I have played around with different ways of getting rid of this error but to no avail. Any help is appreciated.

    This isn't the full program just what I whittled down to show the error.
    Code:
    //deck of pla8ing cards
    #include <stdio.h>
    #include <stdlib.h>
    
    #define CARDS_PER_HAND 5
    
    div_t *dealHand(void);
    
    ////////////////////////////////
    /////////   Main Program 
    int main(int argc, char *argv[])
    {
    div_t player[5];
    
    	player = dealHand();
    	//player=sortHand(player);
        
     	return 0;
    }
    
    //////deal hand //////////////
    div_t *dealHand(void) {
    div_t hand[5];
    
    	for(i=0;i< CARDS_PER_HAND;i++) {
             hand[i]= div(i * 30,13); 
    
            printf("%d\n",hand[i].quot,hand[i].rem); 
    
         }
    
         return hand ;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Using div and div_t like that is not really worth it. You should just make your own struct with names that make sense for your program.

    Here's an example of passing an array to a function:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define HANDSIZE 5
    
    typedef struct Card {
        int value;
        int suit;
    } Card;
    
    void dealHand(Card *hand) {
        int used[52] = {0};
        for (int i = 0; i < HANDSIZE; i++) {
            int n;
            do {
                n = rand() % 52;
            } while (used[n]);
            used[n] = 1;
            hand[i].value = n % 13 + 1;
            hand[i].suit = n / 13;
        }
    }
    
    int main() {
        srand(time(NULL));
        Card hand[5];
        dealHand(hand);
        for (int i = 0; i < HANDSIZE; i++)
            printf("%c%c ", "xA23456789TJQK"[hand[i].value],
                            "CDHS"[hand[i].suit]);
        putchar('\n');
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    33
    Yes, that way is better for using more descriptive variable names.

    I have not seen this use before "xA23456789TJQK"[hand[i].value] but I like it. Just a substitution.

    Thanks for your help. Impressive how quickly you came up with this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble returning char array by function
    By JordanWatson in forum C Programming
    Replies: 1
    Last Post: 03-03-2015, 02:05 PM
  2. trouble with arrays returning false values
    By mad_muppet in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2012, 04:09 AM
  3. Having trouble returning a structure.
    By awr7126 in forum C Programming
    Replies: 7
    Last Post: 03-28-2011, 05:08 PM
  4. trouble returning an object by reference
    By fisheromen1031 in forum C++ Programming
    Replies: 14
    Last Post: 02-19-2008, 11:10 PM
  5. div_t to integer
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 06-24-2002, 03:09 PM

Tags for this Thread