Hi Guys

I was doing some reading up on data structures to refresh my memory when I came across the following:

pmovie->title
*pmovie.title

source: http://www.cplusplus.com/doc/tutorial/structures/

pmovie is pointing to an instant of a struct. I know the arrow operator is a dereference operator for objects and I thought *pmovie.title was exactly the same thing but it isnt. So whats the difference between the two?

>So whats the difference between the two?
The member operator binds more tightly than the indirection operator, so the net effect of *pmovie.title is *(pmovie.title) . To match pmovie->title you need to force the indirection first with (*pmovie).title

It's an order of operations thing. The '*' dereference operator has very low priority compared to other operations. You need to use parentheses to explicitly control the order of operations.

*pmovie.title accesses then dereferences the "title" element of the struct, which most likely is not a pointer and thus causes an error.

The proper version is (*pmovie).title which dereferences pmovie, then accesses the "title" element of the dereferenced object.

The page you linked has most of this information right on it.

[edit]
looks like Narue beat me to it...

Thanks guys. I did read it a few times but couldnt get my head around it.
Ok, I have a question about the address/pointer type operators. See examples below:

int myFunct(string& aVal) { ... }
int myFunct2(string * aVal2) { ... }

Regarding myFunct2(), aVal2 will accept a pointer of type string but Im not too sure about aVal in myFunct(). I assume aVal accepts an address of type string. Is this correct ?

Technically, you are correct it receives an address that it must link the parameter variable to. The "reference" version is an implied pointer.

This allows you to use the actual variable as an argument as well as within the function without the dereferencing operator(s).

The net effect is the same, but the technique/actions required are different.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.