First, to be safe one should not dereference a pointer without checking it first. doublepointer is a pointer, it just happens to point to a pointer. It could be null, which is to say you may need to protect against a crash with
Code:
if ( doublepointer == NULL ) return;
This is in the "novice" form. We may well say "if ( !doublepointer )", but at least the "novice" form is clear.
It checks to see that doublepointer is valid before we dereference it in the next line
Code:
while (*doublepointer != NULL)
When I read further I see "ptrptr" used, but not declared, so I'm not sure what that should say, so I expect a crash (or that it is probably doublepointer), but that ignores the fact that this is not a valid "while" loop. When, exactly, should pre_insert be called? Is it to be called when the "while" finishes, or is it to be called at each loop? It can't go where it is, that is a syntax error relative to the "while" loop syntax.
Setting that aside we come to:
Code:
doublepointer = &((*doublepointer)->next_ptr);
On first look it seems unwise to use doublepointer here. I'll get to that in a moment.
What this says is that the current value of (*doublepointer) is a pointer to a list. If I had list *p, and it were earlier set with p = *doublepointer, this interior expression would be (p->next_ptr), which is a little easier to understand. It is the same as (*doublepointer)->next_ptr.
That evaluates to a list pointer. That pointer has an address, which can be give with &((*doublepointer)->next_ptr), which is the same as &(p->next_ptr) given p from the previous example.
This address is the address of a pointer, compatible with the "pointer to a pointer" of doublepointer.
Yet, that is confusing. It works, fine, but isn't what you are actually doing.
What you are actually doing is more like this:
Code:
list *p = *doublepointer;
while ( p != NULL) {
p = p->next_ptr;
}; // notice the ';' I put here
This loops through the list until p is NULL, but I doubt that's what you really need.
When p is NULL, what do you have? You have NULL.
Is the purpose not to find the last VALID p?
Code:
list *p = *doublepointer;
while ( p != NULL && p->next != NULL) {
p = p->next_ptr;
}; // notice the ';' I put here
Now the list refuses to loop if p is already null (and you have no list anyway).
However, it stops when the next p is null, leaving you with the last valid p, the last "list *" in the series. It is then ready for the "new" list * to be appended.