Search code examples
cpointer-to-pointer

How can a double pointer be used for a two dimensional matrix?


I am trying my hand at C by implementing Conway's game of Life.

I am trying to dynamically build two grids (int matrices), one for the current and one for the next generation, so after I determine what the next generation looks like, I just swap pointers.

At first I tried hopelessly to define the pointer to the grid like int * grid, which you cannot subscript with a second set of brackets like [][] because - obviously - the first set of brackets returns an int.

I also tried something like int * grid[HEIGHT][WIDTH], but this gives problems assigning one pointer like this to another. (And in fact, I have no idea what this really does in memory!)

In my naïve hopefulness, I thought the following could work after stumbling across double pointers. The program compiles, but fails when running on the line indicated. (In Windows, I get no more detail other than that the Problem Event Name is APPCRASH).

DISCLAIMER: This is not the actual program, just a proof of concept for the problem.

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

int HEIGHT = 20;
int WIDTH = 20;

int ** curr_gen; // Current generation
int ** next_gen; // Next generation

/* Entry Point main */
int main(int argc, char** argv) {

    // Allocate memory for the grids
    curr_gen = malloc(sizeof (int) * WIDTH * HEIGHT);
    next_gen = malloc(sizeof (int) * WIDTH * HEIGHT);

    curr_gen[0][0] = 0; //<< PROGRAM FAILS HERE

    // Release heap resources
    free(curr_gen);
    free(next_gen);

    return 0;
}

Solution

  • A common way to do this is described in http://c-faq.com/aryptr/dynmuldimary.html