-2

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;
 }
drhunn
  • 21
  • 1
  • 2
  • 13
  • 4
    Without understanding your code, the double `k++` looks suspicious. – unwind Feb 25 '14 at 13:09
  • 2
    Run in a debugger. It will stop when the crash happen and let you examine the function call stack as well as let you walk up the call stack. Go up to your code (if you're not there already) and you can print the values of variables. Of course, you have to build with debug info (`-g` flags to gcc/clang). If nothing else, please edit your question to include the function call stack when the crash happens. – Some programmer dude Feb 25 '14 at 13:10
  • Please format your code. – Jabberwocky Feb 25 '14 at 13:10
  • 2
    `char *rankTemp = rank[i];` shouldn't it be `rank[j]` ? – Doraj Feb 25 '14 at 13:13
  • My apologizes I am coming from java and just starting to learn c – drhunn Feb 25 '14 at 13:30

2 Answers2

5

You can't use strcat(rankTemp, suitTemp); because rankTemp pointing to a string literal, by doing this you will modify string literal that illegal memory instruction, and an invalid access to valid memory can be detected by OS then os sends SIGSEGV that causes core dunmp.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • crashing at strcat(rankTemp, suitTemp); Any suggestion on how to fix? – drhunn Feb 25 '14 at 13:23
  • @drhunn yes, first change [`char* str[]` to `char str[][]`](http://stackoverflow.com/a/17661444/1673391) keep sufficiently large lenght that new string can be concated and read also [this answer](http://stackoverflow.com/a/16751011/1673391) – Grijesh Chauhan Feb 25 '14 at 13:25
1

I think your problem is here

void swap(char *first,char *second)// swapping pointers
{
char temp = *first;
*first = *second;
*second = temp;

}

should be

void swap(char *first,char *second)// swapping pointers
{
char * temp = first;
first = second;
second = temp;

}
CucumisSativus
  • 310
  • 3
  • 11