🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.c.moderated
5 messages
5 total messages Started by Terry Heath Sat, 23 Mar 1996 06:45
Newbie Question: Multiple indirection
#2360
Author: Terry Heath
Date: Sat, 23 Mar 1996 06:45
8 lines
664 bytes
I have seen many references in text  I have read about multiple
indirection (i.e. **p); I seen how to initilize them and so on.  The
question is:

  Why would it be necessary to use such a pointer, and when?

Thanks
Terry
Newbie Question: Multiple indirection
#2370
Author: Erik W. Anderson
Date: Sun, 24 Mar 1996 17:41
38 lines
1654 bytes
Terry Heath wrote:
> I have seen many references in text  I have read about multiple
> indirection (i.e. **p); I seen how to initilize them and so on.  The
> question is:
> 
>   Why would it be necessary to use such a pointer, and when?

/* all credit for this should go to Al Stevens of DJ fame */

#include <stdio.h>

static char *poem[] = {
  " TRAVELLER'S CURSE AFTER MISDIRECTION\n",
  "May they stumble, stage by stage",
  "On an endless pilgrimage,",
  "Dawn and dusk, mile after mile,",
  "At each and every step withal",
  "May they catch their feet and fall;",
  "At each and every fall they take",
  "May a bone within them break;",
  "And may the bone that breaks within",
  "Not be, for variation's sake,",
  "Now rib, now thigh, now arm, now shin,",
  "But always, without fail, THE NECK.\n",
  "                  Robert Graves",
  NULL
};

int main ()
{
  char **cp = poem ;

  while (*cp) puts (*cp++) ;
  return 0 ;
}

=======================================================================To moderator: please excuse this intrusion. If you could post or e-mail
the FAQ for this group, I would be more than willing to read it. Thanks!
Re: Newbie Question: Multiple indirection
#2372
Author: Kurt Watzka
Date: Sun, 24 Mar 1996 17:42
52 lines
2221 bytes
cn3780@coastalnet.com (Terry Heath) writes:

>I have seen many references in text  I have read about multiple
>indirection (i.e. **p); I seen how to initilize them and so on.  The
>question is:

>  Why would it be necessary to use such a pointer, and when?

Scenario 1:

  You have a sorted linked list, and you want to insert a new node.
  Since the new node can become the new "head" of the list, you
  have to pass a pointer to the "head" pointer to your insert
  function.

Scenario 2:

  A function allocates memory for one of it's results, but the
  result of the function is needed for something else. To
  modify a pointer in this function, you have to pass a pointer
  to that pointer to a pointer to that function.

Scenario 3:

  (strtol() and friends) You pass a string to a scanner, and the 
  scanner scans one item from that string. The evaluated result
  of scanning the string from the beginning is returned as a
  function result. To work with the rest of the string, you have to
  know how many characters in the string have been consumed by
  the scanner. You mimic passing a pointer to the first character
  that was _not_ recognized by the scanner to that function by
  reference.

Scenario 4:

  You want to work with matrices where both the number of rows and
  the number of columns are not known at compile time. You still
  want to use "array[i][j]" notation when you use the matrix.

Summary: 

  Two basic uses for pointers to pointers are:

  - Simulating "pass by reference" for a pointer type and
  - Two dimensional dynamic allocation.

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-6254
| watzka@stat.uni-muenchen.de
| ua302aa@sunmail.lrz-muenchen.de
Re: Newbie Question: Multiple indirection
#2373
Author: Erik Anderson
Date: Sun, 24 Mar 1996 17:43
17 lines
1368 bytes
In article <4j06lv$7ve@solutions.solon.com>,
   cn3780@coastalnet.com (Terry Heath) wrote:
>I have seen many references in text  I have read about multiple
>indirection (i.e. **p); I seen how to initilize them and so on.  The
>question is:
>
>  Why would it be necessary to use such a pointer, and when?

Multiple indirection is usually needed when you are changing a pointer from 
within a function.  For example, if you were to open a FILE from inside a 
function, you would need pass to it a double-pointer, so that the changes can 
escape.  Also, if you are dealing with a linked list, and you are calling a 
function that may change what the head of the list is, then you are also going 
to have to pass a double pointer.

Another reason for using multiple indirection is when you are dynamically 
allocating your own multi-dimentional array.
Re: Newbie Question: Multiple indirection
#2378
Author: Brian V. McGroarty
Date: Sun, 24 Mar 1996 17:48
59 lines
2088 bytes

Terry Heath wrote:
>I have seen many references in text  I have read about multiple
>indirection (i.e. **p); I seen how to initilize them and so on.  The
>question is:

>  Why would it be necessary to use such a pointer, and when?


Consider a case where you need to create a 2 dimensional array whose size
in each of the 2 dimensions is not known at compile time.  Examples are
matrix math functions or arrays of strings.

With double indirection, you can make "anytype **mainPointer" a pointer to
an array of pointers to rows of data, fill each of the pointers to rows of
data with a pointer to a row of data and access the data with bracket
operators as though it were a standard array.

Example:

char **forceField;

void DoLittle( void )
{
    AnyFunction( 10, 20 );
}

void AnyFunction( int rows, int columns )
{
    int allocLoop;

    forceField= (char **)malloc( sizeof( char * ) * rows );
    /* insert check to see you got !NULL, deal with failures */

    for( allocLoop= 0; allocLoop< rows; allocLoop++ )
    {
        forceField[ allocLoop ]= (char *)malloc( sizeof( char ) * columns );
        /* insert check to see you got !NULL, deal with failures */
    }        

    /* now you can do the following */

    forceField[ 5 ][ 6 ]= 1;
    forceField[ 0 ][ 0 ]++;

    /* clean up */
    for( allocLoop= 0; allocLoop< rows; allocLoop++ )
    {
        free( forceField[ allocLoop ] );
    }        
    free( forceField );
}



---
Brian Valters McGroarty -- brianmcg@bix.com
phone/fax (847) 439-7714
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads