AllExperts > Experts 
Search      

C++

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More C++ Answers
Question Library

Ask a question about C++
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Saikrishna
Expertise
I can answer questions in C (Basic and a little of advanced). I can't answer questions related to multi threading

Experience
2.6 years

Education/Credentials
I am a B-tech graduate majored in computer science.

 
   

You are here:  Experts > Computing/Technology > C/C++ > C++ > complicated syntax

Topic: C++



Expert: Saikrishna
Date: 5/14/2008
Subject: complicated syntax

Question

hello
i was working with linked lists and dynamic memory
when i add a new node to the list
i have the following function prototype

void addnode(structtype*& head)
{
//code goes here
}

what does the *& mean?

thanks

Answer
*& is a pointer variable containing the reference of the
address. so any modification to this will modify the variable
accordingly
An example would make it clear:

   void allocate(int *x)
{
x  = new int;
}
main()
{
int *p;
allocate(p);
*p = 6;
}

The above code snippet results in an access violation error stating
"data space never allocated" . This is because the address to the new
int is never passed back to main. so the main will have no clue of
where p is.

consider the below snippet:

void allocate(int *&x)
{
x = new int;
}
main()
{
int *p;
allocate(p);
*p = 1;
}

Here the pointer contains the reference of the address of the variable
x.so when the address is captured in p and memory is alloacted at a
particular location the main is aware of all this becuase it has
called the allocate function using a reference. (which contains a
reference parameter or an alias of the variable being passed from
main). So the address is returned back to main.


Does this clarify your doubt?


Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.