0

I am attempting to make a program to track a collection of baseball cards. I'm simulating buying packs of 7 cards and am trying to complete the total collection (which contains 500 unique cards). To do this, I assume that each card has a value from 0-499 and I've created a function to simulate buying packs of 7 cards with unique numbers in each pack. However, the program doesn't seem to work and ends up crashing. Some help would be much appreciated. Here is my program (which isn't entirely completed yet):

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


int (*newDeck(int n)) //My function to generate a pack of 7 cards
{
             int b[n];
             int *(deck); 
             deck = &b;
             int rn;


             for (int i = 0; i < 7; i++)
             {
                 rn = rand();
                 deck[i] = rn%500;
             }


        return deck;
}



int main()
{

        int collection[500][2], *deck;

        for (int i = 0; i < 500; i++) //Populating the array of the entire collection
        {
            collection[i][0] = i;
            collection[1][1] = 0;
        }

        srand(time(NULL));   //Calling the function and filling it with random numbers
        deck = *newDeck(7);
        printf("%d\n", *newDeck(7));



        for (int i = 0; i < 7; i++) // Adding the numbers generated to the entire collection
        {
            (collection[deck[i]][1])++;
        }
    }


return 0; //There's more to do but that's all I've done so far and it doesn't work
}
Recker
  • 1,915
  • 25
  • 55

2 Answers2

0

In the newDeck() function, replace

int b[n];

with

int *b = malloc(sizeof(int) * b);

Your current method returns the initialized deck in memory that was allocated on the stack when newDeck() was called. This memory is thrown away when the function returns. Thus, to get this to work, you must call malloc() which will permanently keep the memory around until you call free() on the pointer later - perhaps once you are finished working with the deck.

Chris McGrath
  • 1,936
  • 16
  • 17
0

You really need a good C book, and read it very carefully.

Answers to The Definitive C Book Guide and List give some very good advices on book choice, special this answer.

Anyway, here is a revised version of your codes:

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

int *newDeck(size_t n)
{
    int *deck; 

    if ((deck = malloc(sizeof(int) * n)) == NULL)
    {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    srand(time(NULL));
    for (size_t i = 0; i < n; i++)
    {
        deck[i] = rand() % 500;
    }

    return deck;
}

int main()
{
    int collection[500][2], *deck;

    for (int i = 0; i < 500; i++) //Populating the array of the entire collection
    {
        collection[i][0] = i;
        collection[i][1] = 0;
    }

    deck = newDeck(7);
    for (int i = 0; i < 7; i++)
    {
        printf("%d\n", deck[i]);
    }

    for (int i = 0; i < 7; i++) // Adding the numbers generated to the entire collection
    {
        (collection[deck[i]][1])++;
    }

    free(deck); // Does not need deck anymore.

    exit(EXIT_SUCCESS);
}
Community
  • 1
  • 1
Lee Duhem
  • 14,695
  • 3
  • 29
  • 47