delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/05/12:24:53

Date: Wed, 5 Feb 1997 12:06:12 -0500 (EST)
From: Nikita Proskourine <nproskou AT goucher DOT edu>
To: Christian Bird <cbird AT byu DOT edu>
cc: djgpp AT delorie DOT com
Subject: Re: Dynamically allocated arrays
In-Reply-To: <32F51954.6B69@byu.edu>
Message-ID: <Pine.SUN.3.91.970205120111.2268B-100000@rwd>
MIME-Version: 1.0

On Sun, 2 Feb 1997, Christian Bird wrote:

> struct cell
> {
>         int left, up, right, down, used;
> };
> 
> cell ** maze;
I assume you meant cell ** make_maze;

> int i;
> 
> make_maze = (cell **) malloc(max_width*sizeof(cell *));

> for (i = 0; i < max_width; i++) make_maze[i] = (cell *)   
> malloc(max_height*sizeof(cell));

Why not forget pointers-to-pointers and implement a simpler 1D array 
with cell *

make_maze = (cell *)malloc(max_width*max_height*sizeof(cell));

Then you can just pass make_maze to a function and use it as a 1D or 2D 
array.

Also make sure you free(make_maze) when you don't need it.

-- Nikita.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019