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 6614963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:24:12+00:00 2026-05-25T20:24:12+00:00

I’ve been trying to wrap my head around this the whole day… The code

  • 0

I’ve been trying to wrap my head around this the whole day…

The code that I have so far works as planed, the idea is that I’ll have to change tCell * cells[3][5]; to take a size that’s given at runtime. What changes do I need to make to retain the functionality?

typedef struct {
  int active;

} tCell;

typedef struct {
  tCell * cells[3][5];

} tGrid;

// creates a grid and initialize all the cells to NULL
tGrid *init_grid()
{
  tGrid *grid= malloc(sizeof(tGrid));

  if(grid == NULL)
      exit(127); // failed to malloc

  int i, j;
  for(i=0; i < 3; i++)
    for(j=0; j < 5; j++)
      grid->cells[i][j]= NULL;

  return grid;
}

// adds a cell to the grid
void add_cell(tGrid *grid)
{
    tCell cell;

    int y = rand() % 4;

    if(grid->cells[0][y] != NULL)
        exit(127); // cell is taken

    cell.active = 1;
    grid->cells[0][y] = &cell;
}

void remove_cell(tGrid *grid, int x, int y)
{
    if(x < 0 || x > 3 || y < 0 || y > 5)
        exit(127); // out of bounds

    grid->cells[x][y]= NULL;
}

Basically, init_grid will have to take x and y as parameters:

tGrid *init_grid(int x, int y);

But then, how do I change tGrid struct definition? Whatever I’ve tried so far yielded a compiler error (e.g. tCell * cells[][];)

On a slightly related note, how do you read “tCell * cells[3][5];” outloud?

Note:

  • this is a C question
  • I’m using gcc 4.1 to compile the code
  • 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-05-25T20:24:13+00:00Added an answer on May 25, 2026 at 8:24 pm

    Easy.

    typedef struct {
      int rows;
      int columns;
      tCell **cells;
    } tGrid;
    

    And allocating:

    tGrid *pGrid = (pGrid*)malloc(sizeof(tGrid));
    /* check results etc */
    pGrid->rows = rows;
    pGrid->columns = columns;
    pGrid->cells = (tCell**)malloc(sizeof(tCell*)*rows);
    /* check results */
    do{
        pGrid->cells[rows-1] = (tCell*)malloc(sizeof(tCell)*columns);
        /* check results */
    } while (--rows);
    

    Done.

    Or, you can also do:

    typedef struct {
      int rows;
      int columns;
      tCell *cells;
    } tGrid;
    /*****whatever in the middle ***********/
    pGrid->cells = (tCell*)malloc(sizeof(tCell)*rows*columns);
    

    instead of the do-while loop. The difference is that in the first case, each row will be a separate array in the memory, which may be useful when handling the thing.

    Of course, in the end, for each malloc there has to be a free.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

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.