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. Function parameter question

Function parameter question

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 5 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
    econy
    wrote on last edited by
    #1

    struct node{
    int key;
    node* head;
    }

    node *head = NULL;
    void func(node* &h,int n, int m)
    {
    if(n > m)
    return;
    else {
    int mid = (n+m)/2;
    func(h->head,n,mid-1);
    func(h->head,mid+1,m);
    }
    }

    I am wonder what is the difference of parameter: func(node* &h, int n, int m) and func(node* h,int n, int m) what is the usefulness of node *&h.

    D C 2 Replies Last reply
    0
    • E econy

      struct node{
      int key;
      node* head;
      }

      node *head = NULL;
      void func(node* &h,int n, int m)
      {
      if(n > m)
      return;
      else {
      int mid = (n+m)/2;
      func(h->head,n,mid-1);
      func(h->head,mid+1,m);
      }
      }

      I am wonder what is the difference of parameter: func(node* &h, int n, int m) and func(node* h,int n, int m) what is the usefulness of node *&h.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      econy wrote:

      what is the usefulness of node *&h.

      It is a pointer to a reference. You can modify the pointer itself rather than the object that the pointer is pointing to.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      E Richard Andrew x64R 2 Replies Last reply
      0
      • D David Crow

        econy wrote:

        what is the usefulness of node *&h.

        It is a pointer to a reference. You can modify the pointer itself rather than the object that the pointer is pointing to.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        E Offline
        E Offline
        econy
        wrote on last edited by
        #3

        Then use pointer is not the same thing ?

        1 Reply Last reply
        0
        • D David Crow

          econy wrote:

          what is the usefulness of node *&h.

          It is a pointer to a reference. You can modify the pointer itself rather than the object that the pointer is pointing to.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          DavidCrow wrote:

          It is a pointer to a reference.

          Please forgive me if I'm wrong, but isn't it a reference to a pointer, instead?

          The difficult we do right away... ...the impossible takes slightly longer.

          D 1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            DavidCrow wrote:

            It is a pointer to a reference.

            Please forgive me if I'm wrong, but isn't it a reference to a pointer, instead?

            The difficult we do right away... ...the impossible takes slightly longer.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Possibly. I've actually never used either (if both are valid concepts) so I could be wrong.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            L 1 Reply Last reply
            0
            • D David Crow

              Possibly. I've actually never used either (if both are valid concepts) so I could be wrong.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

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

              Pointer to reference is not valid, since a reference is not an object.

              1 Reply Last reply
              0
              • E econy

                struct node{
                int key;
                node* head;
                }

                node *head = NULL;
                void func(node* &h,int n, int m)
                {
                if(n > m)
                return;
                else {
                int mid = (n+m)/2;
                func(h->head,n,mid-1);
                func(h->head,mid+1,m);
                }
                }

                I am wonder what is the difference of parameter: func(node* &h, int n, int m) and func(node* h,int n, int m) what is the usefulness of node *&h.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                func(node* &h, int n, int m)

                h is a INOUT parameter, if func changes h then the caller see the changed value (that is after function execution, h could point to another address).

                func(node* h,int n, int m)

                h is a IN parameter, if func changes it, the caller doesn't see the changed value.

                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