example about something

hey folks i just need example about

Expression What is evaluated Equivalent
a.b Member b of object a
a->b Member b of object pointed by a (*a).b
*a.b Value pointed by member b of object a *(a.b)

u may consider a,b with any values also any other variables and addresses :)
Last edited on
Looks like a copypaste from an assignment sheet.
no its from tut -.-
It's from the data structures section on this site's tutorial:

http://cplusplus.com/doc/tutorial/structures/
yes , so ? no one can give an example about it ?
There is an example right above it? :/
Think of a linked list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct listItem {
  int value;
  listItem *next;
}

listItem a; // Object of type listItem
listItem *pa; // Pointer to object of type listItem
// Print the value:
cout << a.value; // a is an object: 'dot' notation.
cout << pa->value; // pa is a pointer: 'arrow' notation.
cout << (*pa).value; // (*pa) is an object: 'dot' notation
// Get the next item
listItem *pnext1 = a.next; // a is an object, a.next is a pointer to a listItem
listItem *pnext2 = pa->next; // pa is a pointer, pa->next is a pointer to a listItem
listItem next1 = *(a.next); // a.next is a pointer, *(a.next) is an object
listItem next2 = *a.next; // Equivalent to above
listItem next3 = *(pa->next); // pa->next is a pointer, *(pa->next) is an object
listItem next4 = *((*pa).next); // (*pa).next is a pointer, *((*pa).next) is an object 
listItem next5 = *(*pa).next; // Equivalent to above 

All you really need to remember: getting a member of a class/struct is done by the 'dot' notation, while getting a member of a pointer to a class/struct is done by the 'arrow' notation. Also, the '*' operator works on the entire variable, even if it's a member of a member of a member of a .... of a struct/class. To dereference a single step, put it between brackets.
Topic archived. No new replies allowed.