delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/01/21/17:45:39

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: "Jake" <LightningRider AT 1st DOT net>
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
References: <ZNN28.965$903 DOT 5465 AT news> <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: <Kk138.1004$903.5454@news>
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 <pfiland AT mindspring DOT com> 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 <stdio.h>
> > 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<columns;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<rows; y++){
> >    for (x=0; x < columns; x++)
> >      printf("%3d",arr[x][y]);
> >    puts("");
> >  }
> >  puts("");
> > }
> >
> > void initarray(int **arr)
> > {
> >  int x;
> >   for (x=0;x<columns;x++)
> >     arr[x] = (int *)calloc(rows, sizeof(int));
> > }
>
> /* BEGIN new.c */
>
> #include <stdio.h>
> #include <stdlib.h>
>
> 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


- Raw text -


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