1

I have a linked list like this:

typedef struct _LIST_ELEMENT
{
    T* data;
    _LIST_ELEMENT* nextItem;
    int index;

}LITEM,*P_LITEM,**PP_LITEM;

and four pointers to pointer

PP_LITEM _head;
PP_LITEM _tail;
PP_LITEM _current;
PP_LITEM _previous;

The situation is like this:

I have 4 elements in the list and _head points to the first and _tail to the last, _current and _previous work together as a cursor. After i erase element with index 2, the list contains 3 elements with indexes 0,1 and 3 and all pointers point right. At this point if i do this (to move the cursor to the start):

(*_previous) = (*_tail);
(*_current) = (*_head);

The list is missing element with index 1 but all pointers point correctly. If i do this:

_previous = &(*_tail);
_current = &(*_head);

everything is fine and nothing missing.

If anyone has any idea why is this happening please enlighten me.

17
  • 1
    (_previous) = (*_tail) - this shouldn't even compile! _previous is a pointer-to-pointer, but *_tail is not. I suggest turning up your compiler's warning level. Commented Apr 23, 2012 at 13:26
  • 1
    More generally, it's usually considered bad practice to hide pointers behind typedefs (regardless of whether Microsoft used to do that a lot in Win32 stuff)... Commented Apr 23, 2012 at 13:27
  • 1
    Why do people tend to write C and then tag the question with c++? Commented Apr 23, 2012 at 13:29
  • (_previous) = (*_tail) was wrong i corected Commented Apr 23, 2012 at 13:30
  • because from what i know there are no templates in C Commented Apr 23, 2012 at 13:33

1 Answer 1

4

(*_previous) = (*_tail) and _previous = &(*_tail) mean completely different things.

(*_previous) = (*_tail) means "set the value of the thing pointed to by _previous to be equal to the value of thing pointed to by _tail.

_previous = &(*_tail) is equivalent to _previous = _tail, and so it means "set _previous to point at the same thing as _tail.

Sign up to request clarification or add additional context in comments.

2 Comments

I know that but probably my question was not very clear. When i set the values the nextItem of the element with index 0 is pointing to the element with index 3, so the element with index 1 is missing. in the second case i don't have the index 1 element missing. What i don't understand is why when i set the values one element is missing.
I have elem1->nextItem pointing to elem2, elem2->nextItem to elem3 and elem3->nextItem to elem1. When i do his (_previous) = (_tail) elem1->nextItem is pointing to elem3 and elem3->nextItem is pointing to elem1, *_tail points to elem3 and _head to elem1 so elem2 is gone. if i do this _previous = _tail everything is fine. i don't understand why in the first case elem1->nextItem is changing to point to elem3 even though i made no changes to the nextItem of any element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.