Pointers to pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cman

    Pointers to pointers

    What do pointers to pointers accomplish? How does having a pointer to
    a pointer help a design or aglorithm? Where are they normally used?

    cman

  • Kenny McCormack

    #2
    Re: Pointers to pointers

    In article <1171819744.426 037.176610@s48g 2000cws.googleg roups.com>,
    cman <tilakb@gmail.c omwrote:
    >What do pointers to pointers accomplish? How does having a pointer to
    >a pointer help a design or aglorithm? Where are they normally used?
    >
    >cman
    >
    If you want to modify an object passed in the argument list of a
    function from within the function, you must pass a pointer to the object
    as the argument. So, if you want to modify a pointer, you should pass a
    pointer to the pointer as the function argument.

    Comment

    • Eric Sosman

      #3
      Re: Pointers to pointers

      cman wrote:
      What do pointers to pointers accomplish? How does having a pointer to
      a pointer help a design or aglorithm? Where are they normally used?
      This is just a special case of "What do pointers to (Things)
      accomplish?" The answer is that they provide a way to access
      (Things) without needing to know their names -- indeed, without
      them even needing to have names at all. Sometimes the (Things)
      are ints, sometimes they are doubles, sometimes they are structs.
      And sometimes, sometimes, you may want "anonymous" access to a
      (Thing) that is itself a pointer.

      --
      Eric Sosman
      esosman@acm-dot-org.invalid

      Comment

      • Default User

        #4
        Re: Pointers to pointers

        cman wrote:
        What do pointers to pointers accomplish? How does having a pointer to
        a pointer help a design or aglorithm? Where are they normally used?
        Smells like homework. What are your thoughts on the matter? What have
        you found about pointers to pointers in your text?




        Brian

        Comment

        • Tejas Kokje

          #5
          Re: Pointers to pointers

          cman wrote:
          What do pointers to pointers accomplish? How does having a pointer to
          a pointer help a design or aglorithm? Where are they normally used?
          >
          cman
          This sounds like a homework question :-). But I'll help you by giving hints.

          Can you write a function which takes two dimensional char arrays and prints
          it contents ? How will you pass two dimensional array to this function ?

          Can you study how passing command line input to a program works in C ?
          something like

          int main(int argc, char **argv)

          On a advanced topic, can you write a function which inserts a node at the
          head of link list ? How will you pass head pointer to this function ?

          Cheers,
          Tejas Kokje


          Comment

          • August Karlstrom

            #6
            Re: Pointers to pointers

            cman skrev:
            What do pointers to pointers accomplish?
            Nothing really.
            How does having a pointer to
            a pointer help a design or aglorithm?
            Well, it doesn't.
            Where are they normally used?
            Since C lacks call by reference, when you want a function to modify a
            pointer you have to pass it a pointer to the pointer.


            August

            Comment

            • Keith Thompson

              #7
              Re: Pointers to pointers

              Tejas Kokje <binarysemaphor e@gmail.comwrit es:
              cman wrote:
              >What do pointers to pointers accomplish? How does having a pointer to
              >a pointer help a design or aglorithm? Where are they normally used?
              >>
              >cman
              >
              This sounds like a homework question :-). But I'll help you by giving hints.
              >
              Can you write a function which takes two dimensional char arrays and prints
              it contents ?
              [...]

              Not really. A two dimensional array is an array of arrays. C doesn't
              provide a good way of passing such a beast to a function, even using
              pointers, unless the second dimension is fixed. You can pass a
              pointer to the first element and do the indexing calculations yourself
              (though it could be argued that that invokes undefined behavior).

              What you can do is create something that *acts like* a two-dimensional
              array. One obvious way to do this is to create an array of pointers,
              where each pointer points to the first element of a row of the
              "array". This is actually more flexible than a two-dimensional array,
              in that it can be "ragged" (not all the rows have to be of the same
              length) -- but it's more difficult because you have to manage the
              memory allocation for each row *and* for the array of pointers. The
              argv parameter to main() is an example of this.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • CBFalconer

                #8
                Re: Pointers to pointers

                Tejas Kokje wrote:
                cman wrote:
                >
                >What do pointers to pointers accomplish? How does having a pointer to
                >a pointer help a design or aglorithm? Where are they normally used?
                >
                This sounds like a homework question :-). But I'll help you by giving
                hints.
                >
                .... snip ...
                >
                On a advanced topic, can you write a function which inserts a node at
                the head of link list ? How will you pass head pointer to this
                function ?
                I will avoid the confusion of double stars with:

                link *inserthead(lin k* root, whatever data);

                and the usage is:

                root = inserthead(root , data);

                and the code for inserthead probably looks like:

                link *inserthead(lin k *root, whatever data);

                link *tmp;

                if (tmp = malloc(sizeof *tmp) {
                tmp->next = root;
                /* the following may require further mallocation */
                /* depending on the definition of link. Also */
                /* other fields in *tmp may need initialization */
                tmp->data = data;
                root = tmp;
                else {
                /* optional - announce lack of memory */
                }
                return root;
                }

                If you use NULL to signal error, then you want to treat inserthead
                in the same manner as realloc, by receiving its return value in a
                temporary, and copying that to root only if it is non-NULL.

                --
                <http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
                <http://www.securityfoc us.com/columnists/423>

                "A man who is right every time is not likely to do very much."
                -- Francis Crick, co-discover of DNA
                "There is nothing more amazing than stupidity in action."
                -- Thomas Matthews


                Comment

                • Tejas Kokje

                  #9
                  Re: Pointers to pointers

                  On Feb 18, 3:56 pm, CBFalconer <cbfalco...@yah oo.comwrote:
                  Tejas Kokje wrote:
                  cman wrote:
                  >
                  What do pointers to pointers accomplish? How does having a pointer to
                  a pointer help a design or aglorithm? Where are they normally used?
                  >
                  This sounds like a homework question :-). But I'll help you by giving
                  hints.
                  >
                  ... snip ...
                  >
                  On a advanced topic, can you write a function which inserts a node at
                  the head of link list ? How will you pass head pointer to this
                  function ?
                  >
                  I will avoid the confusion of double stars with:
                  >
                  link *inserthead(lin k* root, whatever data);
                  >
                  and the usage is:
                  >
                  root = inserthead(root , data);
                  >
                  and the code for inserthead probably looks like:
                  >
                  link *inserthead(lin k *root, whatever data);
                  >
                  link *tmp;
                  >
                  if (tmp = malloc(sizeof *tmp) {
                  tmp->next = root;
                  /* the following may require further mallocation */
                  /* depending on the definition of link. Also */
                  /* other fields in *tmp may need initialization */
                  tmp->data = data;
                  root = tmp;
                  else {
                  /* optional - announce lack of memory */
                  }
                  return root;
                  }
                  >
                  If you use NULL to signal error, then you want to treat inserthead
                  in the same manner as realloc, by receiving its return value in a
                  temporary, and copying that to root only if it is non-NULL.
                  Point noted. But I was not asking OP to find out ways to avoid using
                  double pointers. In fact, OP was interested in learning about where
                  pointers to pointers can be useful and I as just suggesting him a
                  case.

                  Tejas Kokje



                  Comment

                  Working...