0

I want to delete n'th element from array and when I use for loop I want program to print all numbers by sequence except that n'th element. Ex. 4 7 6 2 9 5.

If I want to delete the 2nd element then after deleting I want to print 4 7 2 9 5 and I don't want to move every element to left.

Is it possible using free() or pointers?

Please explain me, I'm new in pointer programming.

3
  • No it's not possible without moving the elements in the array, an array doesn't have any "holes" in it. It's not even possible with an array allocated dynamically with e.g. malloc. Perhaps arrays is the wrong kind of data-structure whatever problem you want to solve? Commented May 25, 2018 at 10:56
  • 1
    When you mean delete, you mean equal to 0 or suppress the element or only not printing it ? Commented May 25, 2018 at 10:57
  • 1
    I think what you really want is a linked list. Commented May 25, 2018 at 10:59

2 Answers 2

1

No, it is not possible if the data is a C array of integers, e.g.:

int array[10];

However, if you used another data structure, like a linked list, it is possible to remove an element without moving the rest.

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

1 Comment

linked list may be tricky of he does not know how to use free =).
0

You cannot just remove an element of an array using free() You have to opt for Data structures like Linked List in order to do such operations! https://www.geeksforgeeks.org/linked-list-set-3-deleting-node/ Try this !

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.