initalizing two pointers to same value in "for" loop

Posted by MCP on Stack Overflow See other posts from Stack Overflow or by MCP
Published on 2012-03-20T11:26:47Z Indexed on 2012/03/20 11:29 UTC
Read the original article Hit count: 140

Filed under:
|

I'm working with a linked list and am trying to initalize two pointers equal to the "first"/"head" pointer. I'm trying to do this cleanly in a "for" loop. The point of all this being so that I can run two pointers through the linked list, one right behind the other (so that I can modify as needed)...

Something like:

//listHead = main pointer to the linked list
for (blockT *front, *back = listHead; front != NULL; front = front->next)
//...//
back = back->next;

The idea being I can increment front early so that it's one ahead, doing the work, and not incrementing "back" until the bottom of the code block in case I need to backup in order to modify the linked list...

Regardless as to the "why" of this, in addition to the above I've tried:

for (blockT *front = *back = listHead; /.../

for (blockT *front = listHead, blockT *back = listHead; /.../

I would like to avoid pointer to a pointer. Do I just need to initialize these before the loop? As always, thanks!

© Stack Overflow or respective owner

Related posts about c++

Related posts about pointers