CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2006
    Posts
    128

    Question about Double pointers

    I made the mistake in my last post to ask about how "pointers" are helpful.
    And since that went on with a page about how pointers are helpful, I figured I better just ask in a new post since it is completely about something different.

    Instead of asking how "pointers" are helpful, I meant to ask

    "How are DOUBLE pointers helpful", since they are already setup to be helpful in the sense that you can point to something else, or in the case of an array of pointers, easily change the sort easily.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Question about Double pointers

    Well, I believe I answered that question in the previous thread, but anyway.
    In C function arguments as passed by value. So, if you modifies an argument then only the local copy of that argument is modified (not the original value).
    Code:
    void func(double d)
    {
        // this is only seen within this function
        d = ...;
    }
    If you need to modify the original double you can pass a pointer to that double instead.
    Code:
    void func(double *d)
    {
        // dereference pointer-to-double and modify the double. This is seen outside this function.
        *d = ...;
    }
    If you need to modify an pointer to that double, then pass a pointer to the pointer to the double:
    Code:
    void func(double **d)
    {
        // dereference pointer-to-pointer-to-double and modify the pointer-to-double.  This is seen outside this function.
        *d = ...;
    }
    And so on... you can pass pointer-to-pointer-to-pointer-to-double is that is useful...

    In C++ you can pass arguments by reference, so, instead of passing a pointer-to-pointer-to-double you can also pass a reference-to-a-pointer-to-a-double. Like this:
    Code:
    void func(double *&d)
    {
        d = ...; // d is a reference, so this is seen outside the function
    }
    This way you don't have to dereference that pointer within the body of the function.

    A pointer is just pointing to something, and that something might be anoither pointer. So, any pros/cons that applies to a pointer, also applies to a double-pointer (or tripple-pointer). Ok, triple-pointer or quad-pointers and so on are probably a case of poor design...

    - petter

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Question about Double pointers

    conceptually pointer to pointer is important same as pointer to variable.

    Kuphryn

  4. #4
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Question about Double pointers

    Pointer to pointer is rarely a good idea. Usually you can name the thing first pointer points to.
    "Programs must be written for people to read, and only incidentally for machines to execute."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured