X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f From: "Jake" Newsgroups: comp.lang.c,comp.os.msdos.djgpp References: <3C4C2580 DOT 6F2C AT mindspring DOT com> Subject: Re: Pointer Blues Lines: 128 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Message-ID: Date: Mon, 21 Jan 2002 23:18:02 GMT NNTP-Posting-Host: 209.240.10.168 X-Trace: news 1011655082 209.240.10.168 (Mon, 21 Jan 2002 18:18:02 EST) NNTP-Posting-Date: Mon, 21 Jan 2002 18:18:02 EST To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Thanks Pete, Thats what I was looking for. pete wrote in message news:3C4C2580 DOT 6F2C AT mindspring 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