delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/01/21/10:15:09

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: fred AT genesis DOT demon DOT co DOT uk (Lawrence Kirby)
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
Subject: Re: Pointer Blues
Date: Mon, 21 Jan 2002 13:05:47 +0000 (GMT)
Organization: none
Message-ID: <20020121.1305.47599snz@genesis.demon.co.uk>
References: <ZNN28.965$903 DOT 5465 AT news>
NNTP-Posting-Host: genesis.demon.co.uk
X-NNTP-Posting-Host: genesis.demon.co.uk:158.152.9.153
X-Trace: news.demon.co.uk 1011625878 nnrp-10:11421 NO-IDENT genesis.demon.co.uk:158.152.9.153
X-Complaints-To: abuse AT demon DOT net
X-Newsreader: SNews 1.31+mods(djs010919/dpmi)
Lines: 111
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Monday, in article <ZNN28.965$903 DOT 5465 AT news>
     LightningRider AT 1st DOT net "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?

Not necessarily. There are various ways you can use to simulate a 2
dimensional array in C. For example   int array[X][Y]  defines an
array of arrays which is typically considered to be a 2 dimensional array.
It contains no pointer objects. Given  int **p;  p is a pointer to a pointer
to int. It can point to an object that it a pointer to int and can
index over an array of such objects. This can be used to build a
datastructure that simulates a 2 dimensional array, but it is a different
form of datastructure to "array". See section 6.16 of the FAQ for a
description of how to allocate multidimensional arrays in their
different forms.

>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);

You are passing the value of the pointers map and mask which as variables
with static storage duartion here are initialised to null pointers.
Passing a null pointer to initaray() does tell it anything useful.
Since initarray() is allocating the space for the objects and therefore
determining where they will be located it must return this information
somehow. Since arguments in C are passed by value initarray() cannot
alter the values of map and mask in main(). You might write instead:

   map = initarray();
   mask = initarray();

and change initarray() accordingly.



>  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);

It is also a good idea to free memory you have allocated. If this became
part of more complex code you could easily end up with a memory leak.

>  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));
>}

Given how initarray() was caled in main i.e. passing it a null pointer then
arr here will be a null pointer. So arr[x] attemps to dereference a null
pointer which result in undefined behaviour, anything can happen. You
need to allocate an array of pointers before you can access them using
arr[x]. E.g.

int **initarray(void)
{
  int x;
  int **arr = malloc(columns * sizeof *arr);

  for (x=0;x<columns;x++)
    arr[x] = calloc(rows, sizeof **arr);

  return arr;
}

A proper implementation of this should include error checking, both
malloc() and calloc() can return null if they fail.

-- 
-----------------------------------------
Lawrence Kirby | fred AT genesis DOT demon DOT co DOT uk
Wilts, England | 70734 DOT 126 AT compuserve DOT com
-----------------------------------------

- Raw text -


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