0

My struct array has 5 slots

struct router* router[5];

Lets say there are elements inside them, and then i make

router[3] = NULL;

Is it possible to rearrange the array so that the element in router[4] moves up to router[3], router[5] moves to router[4] etc.?

3
  • Of course it's possible. Just use assignment operator or memmove. route[3] = router[4]; router[4] = NULL;. Or please clarify your question if you are really trying to ask something different. Commented Oct 10, 2016 at 19:26
  • Since you probably need to make your code resilient to NULL entries anyway, you might consider doing just that, and leave NULL in the middle of your array. Commented Oct 10, 2016 at 19:29
  • Also note that there is no router[5] element as router[4] is the last valid element in the array. Commented Oct 10, 2016 at 19:30

1 Answer 1

1

Try this

void delete(struct router** router, int which, int size) {
    int i;
    // If these pointers have no other references to them then
    // Then you should free the one being deleted at this point
    for(i = which; i < size - 1; i++) {
       router[i] = router[i + 1];
    }
    router[i] = NULL;

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

Comments

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.