I have no clue where I am going wrong here any assistance would be helpful. I am trying to make a deck of cards out of 2 different array of strings and print it to the console. It compiles fine but when I run it I get "Segmentation fault (core dumped)"
/*
* BlackJack.c
*
* Created on: Feb 25, 2014
* Author: Danny Hunn
* 25 Feb 14 builds a deck for Black Jack
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define NUM_SUITS 4
#define DECK_SIZE 52
#define NUM_RANKS 13
void swap(char *first,char *second)// swapping pointers
{
char temp = *first;
*first = *second;
*second = temp;
}
void shuffle(char *deck[])
{
int seed, gen, i;
seed = (int) time(0);
srand(seed);
for(i =0; i < DECK_SIZE; i++ )
{
gen = rand()%52;
swap(deck[i],deck[gen]);
}
}
void printDeck(char *deck[])
{
int i;
for(i=0;i<DECK_SIZE; i++)
{
printf("%s\n", deck[i]);
}
}
int main(int argc, char *argv[]) {
int deckIndexs = 1;
char *suit[NUM_SUITS] = {" Spades", " Hearts", " Diamonds", " Clubs"};
char *rank[NUM_RANKS] = {"Ace", "Two","Three","Four","Five","Six", "Seven", "Eight", "Nine",
"Ten", "Jack", "Queen", "King"};
char **deck = malloc(deckIndexs * (sizeof(*deck)));
int i,j,k;
k=0;
for(i=0; i< NUM_SUITS; i++)
{
for(j=0; j< NUM_RANKS; j++)
{
char *suitTemp = suit[i];
char *rankTemp = rank[j];
strcat(rankTemp, suitTemp);
deck= realloc(deck, (deckIndexs +1)*sizeof(*deck));// reallocate memory for the size of the array
deckIndexs++;
deck[k] = malloc(254*sizeof(char *));// allocate memory for the new string index
deck[k] = rankTemp;
k++;// increments k for the index of the array
}
}
printDeck(deck);
shuffle(deck);
return 0;
}