Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing double pointer to function

Passing double pointer to function

Scheduled Pinned Locked Moved C / C++ / MFC
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elelont2
    wrote on last edited by
    #1

    Hi I am a bit confused about the double pointer. I would like to pass it to a function and change the pointer value:

    static int GetValue(int** hello)
    {
    *hello = &Something;
    }

    //Why does this work:
    int* pointer = NULL;
    GetValue(&pointer);

    //And this does not work:
    int** pointer = NULL;
    GetValue(pointer);

    To me they seem like the same thing.

    L 1 Reply Last reply
    0
    • E elelont2

      Hi I am a bit confused about the double pointer. I would like to pass it to a function and change the pointer value:

      static int GetValue(int** hello)
      {
      *hello = &Something;
      }

      //Why does this work:
      int* pointer = NULL;
      GetValue(&pointer);

      //And this does not work:
      int** pointer = NULL;
      GetValue(pointer);

      To me they seem like the same thing.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      elelont2 wrote:

      To me they seem like the same thing.

      No, they are totally different. Case 1: You create a pointer that will point to an integer variable, and pass its address to the function. The function then stores the address of a variable inside the pointer. On return the pointer now points to the variable, and the variable contains a value. Case 2: You create a pointer that will point to a pointer but is currently NULL. So when you pass it to the function the function gets a NULL value. Simply put, in case 1, hello points to the address of pointer, in case 2, hello contains NULL. If you step through the code with your debugger it should become clear.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups