Pointer issues

B

bob

In some code I have the following:

1) (*p) ->value;
2) *p->value;

1) gives an error while 2) works. What are the difference between 1)
and 2)?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

In some code I have the following:

1) (*p) ->value;
2) *p->value;

1) gives an error while 2) works. What are the difference between 1)
and 2)?

1 will first try to dereference p and then call the -> on what p points
to, this will only work if what p points to have overloaded that
operator or if p is of type T** (pointer to pointer).

2 on the other hand first uses the -> operator on p and then
dereferences value (meaning that value is a pointer). So another way to
write 2 is *(p-value).
 
J

Jim Langston

bob said:
In some code I have the following:

1) (*p) ->value;
2) *p->value;

1) gives an error while 2) works. What are the difference between 1)
and 2)?

You need to look up "operator precidence". That states what order the
compiler will interpret operators. -> has a higher precidence than *
(dereference). So -> is evaluated first giving you:
*(p->value).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,505
Members
48,192
Latest member
LinwoodFol

Latest Threads

Top