🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2372

Re: Newbie Question: Multiple indirection

#2372
From: 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
Technical Details
Message-ID: <4j41hh$nkd@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4j06lv$7ve@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: watzka@stat.uni-muenchen.de (Kurt Watzka)
Newsgroups: comp.lang.c.moderated
Subject: Re: Newbie Question: Multiple indirection
Date: 24 Mar 1996 11:42:09 -0600
Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4j41hh$nkd@solutions.solon.com>
References: <4j06lv$7ve@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com