You are here:

C++/pointer and address

Advertisement


Question
QUESTION: Hello,

Pointer(*) points to memory address(&), if a method takes an address as its parameter like the following example, why don't we just use pointer (*) in its signature instead of address since we really have to pass in the pointer(*)? and we don't need the symbol & at all in C++ to avoid confusion.

init(DbConnection& conn) {...}

Thanks,
lgczz

ANSWER: (sorry I may have rejected your question initially by mistake when posting it by clicking on the wrong form button - doh!)

You have confused the use of * and & to modify types and their uses as unary operators.

For C and C++:
   * when used with a type indicates a pointer to an object of that type.
   * as a unary operator used with a pointer "dereferences" the pointer to obtain
     a reference to what is pointed to.

   & as a unary operator returns "the address of" an object - i.e. a pointer to an object.

For C++ only:
   & when used with a type indicates a reference to an object of that type and can be considered
     an alias or alternate name for the object it refers to.

C++ Examples:

   T o;          // Defines o as an object of type T - default constructed
   T * p;          // Uninitialised pointer to object of type T.
   
   p = &o;         // p assigned the pointer value that is the "address of" o.

   T * p2( &o );   // p2 defined to be a pointer to an object of
         // type T initialised to point to o.

   T o2;          // Define another object o2 of type T - default constructed.

   *p = o2;        // Assign value of o2 to what p points to (i.e. to o).

   T & r( o );     // r is a reference to an object of type T initialised to refer to o.
         // References *must* be initialised and cannot be assigned to refer
         // to something else, so:
         //      r = o2;
         // does not change the what r refers to. It acts as an assignment of o2 to
         // whatever r refers to, i.e. it acts like:
         //      o = o2;

   *p = r;         // Assign the value of what r refers to (i.e. o)
         // to what p points to (i.e. o2).

   p2 = &r;        // Assign the "address of" the object r refers to to the pointer p2.

So in the example you give:

   init( DbConnection& conn ) {...}

the parameter named conn is a reference to a DbConnection object. The & here is the C++ type modifier signifying "reference to type" and _not_ the address of operator.

If you have not met C++ references before then I suggest you read up on them in a decent C++ text book or reference (no pun intended - honest!). There is also a whole section on references at the C++ FAQ lite at Parashift.com:

   http://www.parashift.com/c++-faq-lite/

Section 8 is on references:

   http://www.parashift.com/c++-faq-lite/references.html .

If after reading up on C++ references you still have questions then please do post another question.

Hope this as helped a bit and pointed you in the direction of further enlightenment if required.


---------- FOLLOW-UP ----------

QUESTION: Thanks for the answer.

Does the signature of init( DbConnection* conn ) have the same meaning of the signature of init( DbConnection& conn )?
what else alternative?

Answer
Did _read_ my answer?

Did you note the C++ use of & for _reference types_?

Did you read the FAQs I pointed you to?

Did you note that T * and T & are _not_ the same types?
(The first being a pointer to type T and the second a reference to type T)

Hence, given the above do you think that:

   DbConnection*

is the same as

   DbConnection&

?

(hint: substitute DbConnection for T in my previous query: "Did you note that T * and T & are _not_ the same types? ").

And from this would the signature of:

   init( DbConnection* conn )

be the same as:

   init( DbConnection& conn )

?

No they are not the same.

The first init is a function taking a (non-const) _pointer_ to a DbConnection.

The second init is a function taking a (non-const) _reference_ to a DbConnection.

Oh, and both are illegal function declarations in (ISO standard) C++. In C++ int is _not_ assumed as a return type if none is specified. So a return type - even int or void - *must* be specified for function declarations (prototypes) and definitions (which are also declarations).

I am sorry I do not understand what you are asking about when you say:

   "what else alternative?"

What is it you wish an alternative to?  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.