Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8069231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:57:18+00:00 2026-06-05T12:57:18+00:00

I came up with a project to work on learning C and I have

  • 0

I came up with a project to work on learning C and I have kind of hit a wall.
The project is simply a card game where the player has two sets of cards, one set is a deck of a set size and the other is their collection of cards that can be as big as it needs to be.
The card structure is as follows:

struct card {   
    char name[256];
    char special[256];
    char type[100];
    char rarity[100];
    int points;
};

I then have a file of the collection called coll.txt

first card goes here 50
second card goes here 70
...

I then have a (sloppy) function that reads from the file and stores it into a temporary card:

void read_into_collection(FILE *f) {
    char *file_text;
    char *token;
    int i;
    struct card temp;

    file_text = (char *) malloc(sizeof(struct card));

    while(fgets(file_text, sizeof(struct card), f)) {
        for(i = 1, token = strtok(file_text, " "); token; i++, token = strtok(NULL, " ")) {
            switch (i) {
            case 1:
                strcpy(temp.name, token);
                break;
            case 2:
                strcpy(temp.special, token);
                break;
            case 3:
                strcpy(temp.type, token);
                break;
            case 4:
                strcpy(temp.rarity, token);
                break;
            case 5:
                temp.points = atoi(token);
                break;
            default:

                i = 0;
                break;
            }
        }
    }


    free(file_text);
}

so by the time i = 6 I am ready to move the temporary card to the collection and read the next card into the temp variable and so on. But how do I do this? I am having trouble figuring out what collection should actually be. At first I thought:

struct card *collection = (struct card *) malloc(number_of_cards * sizeof(struct card));

But, if I am correct, malloc() returns a pointer to a chunk of memory and the memory is not sequential like an array, so I cannot increment the pointer to store cards.

I also tried counting the number of lines in the file (each line is a card) and then making an array of that size but I get an error that the value is not constant.

What is the best way to go about storing these cards as a collection? I would just make the collection a really large array but I feel like this situation comes up often in projects and would rather learn how to handle it rather than take the easy way out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:57:20+00:00Added an answer on June 5, 2026 at 12:57 pm

    But, if I am correct, malloc() returns a pointer to a chunk of memory
    and the memory is not sequential like an array, so I cannot increment
    the pointer to store cards.

    False. It is sequential. You can use malloc() to create an array of anything:

    mystruct* ptr = (mystruct*) malloc(sizeof(mystruct) * numberOfStructs)
    
    for(int i = 0; i < numberOfStructs, i++) {
        ptr[i].setSomeInfo(x);
    }
    

    This is the standard way to do it in C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have MS Visual Studio web project folder came from TFS server with several
I came to a new project which have been developed for a quite long
I have a personal project I'm planning and I came to a small hurdle.
I have some knowledge of CGI scripts and how they work and came to
I'm doing some globalization work in my current project and I came across a
In a rails project I have two entities, Users and Institutions, they have a
I recently came upon a new project with Python as the code-base. I have
I have just started to work on a django project( and learn django at
I have started working on an MVC project and I came across some scenarios
When this quick one hour project came up I never thought that 2 days

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.