Pointers in data structures (Linked List)

What Are all these arrows -> -> ->

Page 1 of 1

6 Replies - 16341 Views - Last Post: 17 October 2005 - 09:22 AM Rate Topic: -----

#1 e_prince_84  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-October 05

Pointers in data structures (Linked List)

Posted 16 October 2005 - 01:37 PM

:pirate: What is the story of all those arrows -> -> -> i don't seem to get it
i was looking at an example of a doubley linked list .. and there was a code
tail->next->previous = tail;

i get that .. but not exactly..
i have a problem with pointer inside a class
Can I get help?

Is This A Good Question/Topic? 0
  • +

Replies To: Pointers in data structures (Linked List)

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Pointers in data structures (Linked List)

Posted 16 October 2005 - 02:09 PM

You did not specify a language, so I took the liberty of moving it to the C/C++ area.

You can certainly get help with your question...can you post it, along with any code you've written?
Was This Post Helpful? 0
  • +
  • -

#3 Videege  Icon User is offline

  • rÍvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: Pointers in data structures (Linked List)

Posted 16 October 2005 - 03:26 PM

-> is an operator used with pointers in C++ (it even looks like an arrow :D). Assuming you were looking at a somewhat standard doubly linked list class,

tail->next->previous = tail;

means that the tail (the last node in the list) node's next node's (in a double linked list, the head node) previous node points to the tail again, creating a circular list.
Was This Post Helpful? 0
  • +
  • -

#4 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Pointers in data structures (Linked List)

Posted 16 October 2005 - 08:12 PM

the -> operator denotes that the right operand is an object. take note that the -> operator will dereference the right operand for you. also,

tail->next->previous = tail


this statement never explicitly states that the tail is pointing to the start of the list. so without having more source it would be hard to tell what is exactly happening with the statement. assuming this is a circular list, it would do nothing, you have just assigned the head node, to the tail node, this should have already been done. if this is not a circular list, then this statement should not work assuming the tail is the last node in the list, there would be no "next" node.
Was This Post Helpful? 0
  • +
  • -

#5 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Pointers in data structures (Linked List)

Posted 16 October 2005 - 10:57 PM

Dark_Nexus, on Oct 17 2005, 09:39 AM, said:

the -> operator denotes that the right operand is an object.

More than just the right operand, it implies that the left operand is a pointer to a structure. It's used as a pointer syntax to solve the problem of operator precedence between the '*' and the '.'.
I'll demonstrate by an example.
Suppose you say the statement
cout<<*ptr.val;

where ptr is a pointer to a structure having val as one of it's data members.
As '.' has a higher precedence than '*' the compiler evaluates the statement as
cout<<*(ptr.val);
which means that print the value of the data contained in the address pointed to by ptr.val
But since ptr is not a structure variable and hence the entire expression is misinterpreted by the compiler.

That's why ANSI came up with the '->' operator which has higher precedence than the '.' operator ( you can confirm it here. )
And that's why the '->' operator is used.

Just remember that for the '.' operator the left operand has to be a structure variable whereas the left operand for the '->' operator has to be a pointer to a structure.

So as I said earlier that the '->' operator was introduced to solve the operator precedence problem, you can even access structure elements via pointers without the '->' operator by forcing the compiler to give the '*' higher priority than '.' And obviously, we use the paranthesis like this:
cout<<(*ptr).val;


this code will compile and works perfectly as the compiler interprets it as:
Print the Data stored in variable val stored in the structure pointed to by ptr.

Makes more sense now...doesn't it??

But since using the '->' operator makes the code look more cleaner than using the paranthesis, everyone uses that now.

This post has been edited by born2c0de: 16 October 2005 - 10:58 PM

Was This Post Helpful? 0
  • +
  • -

#6 wingz198  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 14-October 05

Re: Pointers in data structures (Linked List)

Posted 17 October 2005 - 07:32 AM

I'm probably wrong about this, but the way I look at the '->', is that it's a pointer to a pointer inside a data structure
Was This Post Helpful? 0
  • +
  • -

#7 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Pointers in data structures (Linked List)

Posted 17 October 2005 - 09:22 AM

Quote

I'm probably wrong about this, but the way I look at the '->', is that it's a pointer to a pointer inside a data structure


actually it simply means that the left operand (over here->) is a pointer to an object (ie a structure), and that the right operand (->over here) is a member of that object.

Quote

More than just the right operand, it implies that the left operand is a pointer to a structure


Quote

the -> operator denotes that the right operand is an object.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1