X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f From: pete Newsgroups: comp.lang.c,comp.os.msdos.djgpp Subject: Re: Pointer Blues Date: Mon, 21 Jan 2002 09:28:16 -0500 Organization: PF Lines: 118 Message-ID: <3C4C2580.6F2C@mindspring.com> References: NNTP-Posting-Host: 3f.35.7c.59 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Server-Date: 21 Jan 2002 14:29:37 GMT X-Mailer: Mozilla 3.04Gold (Win95; I) To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Jake wrote: > > Pointers are begining to drive me crazy. > Ok here's the problem, I needed two 2 dimensional arrays that are visual to > all > the functions. So I defined two 'pointer-to-pointer' varibles of type int, > thats what 2 dimensional arrays really are, 'pointers-to-pointer' right? > That problem is the that whatever value I asign to one array shows up in the > array, I can have have this. > If someone can follow the code and know whats going on please help me out. > > thanks > Jake > #include > int columns = 8; > int rows = 8; > int **map; > int **mask; > void initarray(int **arr); // allocates memory > void display(int **arr); > int main(int argc, char *argv[]) > { > int x,y; > initarray(map); > initarray(mask); > for ( y = 0; y < rows;y++) > for (x=0;x map[x][y] = x; > mask[x][y] =x*2; > } > display(map); > display(mask); > return 0; > } > > void display(int **arr) > { > int x,y; > for (y=0; y for (x=0; x < columns; x++) > printf("%3d",arr[x][y]); > puts(""); > } > puts(""); > } > > void initarray(int **arr) > { > int x; > for (x=0;x arr[x] = (int *)calloc(rows, sizeof(int)); > } /* BEGIN new.c */ #include #include int **initarray(int, int); void display(int **, int, int); int main(void) { int x, y, **map, **mask, columns, rows; columns = 16; rows = 8; map = initarray(columns, rows); mask = initarray(columns, rows); for(y = 0; y < rows; ++y){ for(x = 0; x < columns; ++x){ map[x][y] = x + y; mask[x][y] = map[x][y] * 2; } } display(map, columns, rows); free(map); display(mask, columns, rows); free(mask); return 0; } void display(int **arr, int columns, int rows) { int x, y; for(y = 0; y < rows; ++y){ for(x = 0; x < columns; ++x){ printf("%3d",arr[x][y]); } puts(""); } puts(""); } int **initarray(int columns, int rows) { int x, **arr; arr = malloc(columns * sizeof(int*)); if(!arr){ fputs("arr allocation failure.\n", stderr); exit(EXIT_FAILURE); } for(x = 0; x < columns; ++x){ arr[x] = malloc(rows * sizeof(int)); if(!arr[x]){ fprintf(stderr, "arr[%i] allocation failure.\n", x); exit(EXIT_FAILURE); } } return arr; } /* END new.c */ -- pete