0

So I'm creating a program to play go-fish, and I'm starting with shuffling the deck and then printing it. My problems are:

  1. I get a segmentation fault in the "shuffle" function somewhere during assigning the array values.

  2. GCC is complaining about multi-character character constants in "deck". I don't know how else to declare a deck of cards.

  3. I keep getting "Subscripted value is neither array, nor pointer, nor vector" when attempting to print array values.

I know there are probably a lot of obvious faults in my code. Forgive me, please. I'm a beginner.

Here is the code (main function last):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

int shuffle(int deck[52], int version)
{   //this is to store the shuffled deck after it is finished
    int newdeck[52] = {0};

    printf("In shuffle\n");
    //I plan to create other combinations for a shuffled deck, but starting with only one
    switch (version)
    {       //this sets a value in newdeck to a value in deck, obviously mixing the order
            //this is also where I get my segmentation fault.
    case 1: newdeck[0] = deck[26];
            newdeck[1] = deck[13];
            newdeck[2] = deck[33];
            newdeck[3] = deck[45];
            newdeck[4] = deck[19];
            newdeck[5] = deck[36];
            newdeck[6] = deck[42];
            newdeck[7] = deck[24];
            newdeck[8] = deck[51];
            newdeck[9] = deck[22];
            newdeck[10] = deck[15];
            newdeck[11] = deck[10];
            newdeck[12] = deck[3];
            newdeck[13] = deck[6];
            newdeck[14] = deck[43];
            newdeck[15] = deck[48];
            newdeck[16] = deck[27];
            newdeck[17] = deck[18];
            newdeck[18] = deck[1];
            newdeck[19] = deck[31];
            newdeck[20] = deck[14];
            newdeck[21] = deck[0];
            newdeck[22] = deck[21];
            newdeck[23] = deck[12];
            newdeck[24] = deck[41];
            newdeck[25] = deck[35];
            newdeck[26] = deck[16];
            newdeck[27] = deck[4];
            newdeck[28] = deck[9];
            newdeck[29] = deck[40];
            newdeck[30] = deck[25];
            newdeck[31] = deck[34];
            newdeck[32] = deck[44];
            newdeck[33] = deck[7];
            newdeck[34] = deck[5];
            newdeck[35] = deck[38];
            newdeck[36] = deck[46];
            newdeck[37] = deck[37];
            newdeck[38] = deck[49];
            newdeck[39] = deck[39];
            newdeck[40] = deck[23];
            newdeck[41] = deck[50];
            newdeck[42] = deck[20];
            newdeck[43] = deck[2];
            newdeck[44] = deck[11];
            newdeck[45] = deck[47];
            newdeck[46] = deck[28];
            newdeck[47] = deck[17];
            newdeck[48] = deck[30];
            newdeck[49] = deck[8];
            newdeck[50] = deck[29];
            newdeck[51] = deck[32];
            break;
    }
printf("Leaving shuffle\n");
//return the shuffled deck
return newdeck; 
}


int main(void)
{  //declare a deck of cards. 
   int deck = {'2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS', 'AS', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH', 'AH', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC', 'AC', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD', 'AD'};
   //a counter

   int i;
   //this is here for debugging purposes as well as the other similar occurrences

   printf("Entering shuffle\n");

   //shuffles the deck

   deck = shuffle(deck, 1);

   printf("Printing deck\n");
   for ( i = 0 ; i < 52 ; i++ )
   {
      printf("%d ", deck[i]);//print the values in deck (now shuffled and set equal to newdeck from "shuffle" function. I also get the error "Subscripted value is niether array, nor pointer, nor vector."
   }

   return 0;
}
  • How big is an `int` on your machine. How big is a `'xx'` construct? Are things stored in little or big endian? Are you absolutely sure that you know what the compiler is doing here? Are you sure that this is the right way to proceed? Have you considered a more compact coding for rank and suit? – dmckee --- ex-moderator kitten May 22 '14 at 21:45
  • Your program is not compilable. How are you getting segmentation fault? You have to be able to create an executable first and run it to experience segmentation fault. – R Sahu May 22 '14 at 21:48
  • See http://stackoverflow.com/questions/6944730/multiple-characters-in-a-character-constant and http://stackoverflow.com/questions/3960954/c-multicharacter-literal quotes from the c99 standard on the matter (implementation defined). – dmckee --- ex-moderator kitten May 22 '14 at 21:48
  • 1
    Also, for shuffling algorithms, there is a nice article Fisher-Yates method on wikipedia: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle – JohnH May 22 '14 at 21:52
  • I change int deck to int *deck and it compiles. Thanks for the links, I'll look into them. – user2775215 May 22 '14 at 22:02
  • To begin with, change `int deck[52]` to `char* deck[52]`, and those single quotes `'` to double quotes `"`. – barak manos May 22 '14 at 22:27
  • @barakmanos He appears to be intentionally using multi-character constants. Thus the `'xx'` constructs. The problem is that the standard leaves almost everything about the behavior and storage of such things as "implementation defined" leading to massive uncertainty about what is actually going on and a complete lack of portability. If he insists on using them he'll need to go carefully through the documentation for the particular compiler he is using to find out how they work. – dmckee --- ex-moderator kitten May 22 '14 at 22:30
  • @dmckee: yeah, well... I kinda noticed that after submitting the comment... there are several major problems here, making the one that I mentioned less of an issue... – barak manos May 22 '14 at 22:33
  • @barakmanos if he does that then he will have to completely rewrite his shuffle function (not that that's a bad thing). – M.M May 22 '14 at 23:06

1 Answers1

0

The question code attempts to assign:

int deck = {'2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', 'TS', 'JS', 'QS', 'KS', 'AS', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', 'TH', 'JH', 'QH', 'KH', 'AH', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', 'TC', 'JC', 'QC', 'KC', 'AC', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', 'TD', 'JD', 'QD', 'KD', 'AD'};

There are multiple problems with this attempted assignment.

  1. deck is a single integer. Multiple values cannot be assigned to an integer.
  2. The value(s) being assigned are not integers. (ie: 0, 1, 2, 3, -1, -2 ...).
  3. The single quote mark ' is used in C to define a character constant. (ie: 'a', 'b', 'c', 'X', 'Y', 'Z', '1', '2' '3', ...). The character values in the above assignment are illegal in that they are each two characters.

Perhaps this is what was intended:

char *deck[] = { "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD" };

The above defines deck as an array of character pointers. A character pointer may point to the first character of an array (or string) of characters. The array of character pointers are initialized to point to each 3-character string (two visible characters, plus a string termination character '\0'.


The issue outlined above (a limited understanding of C types, pointers, etc.) permeates the question code.

See a spoiler of a 'more correct' version of the question code here.

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
  • 1
    #1 and #3 yes, but #2 no: in C, character constants are `int`. – M.M May 22 '14 at 23:04
  • Multi-character character constants like `'ab'` are perfectly legal in C, and they're of type `int` just like ordinary character constants like `'a'`. The difference is that they have implementation-defined values, are are almost never useful. (You can *probably* assume they'll have distinct value up to `sizeof (int)` characters, but those values can vary from one compiler to another.) – Keith Thompson May 23 '14 at 01:00