Function parameter question
-
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.
-
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.
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
-
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
-
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
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.
-
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.
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
-
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
-
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.
func(node* &h, int n, int m)
h
is aINOUT
parameter, iffunc
changesh
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 aIN
parameter, iffunc
changes it, the caller doesn't see the changed value.