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.

Acorn
  • 24,970
  • 5
  • 40
  • 69
user
  • 33
  • 11
  • 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? – Some programmer dude May 25 '18 at 10:56
  • 1
    When you mean delete, you mean equal to 0 or suppress the element or only not printing it ? – Umaiki May 25 '18 at 10:57
  • 1
    I think what you really want is a [linked list](https://stackoverflow.com/questions/12914917). – Amey Parundekar May 25 '18 at 10:59

2 Answers2

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.

Acorn
  • 24,970
  • 5
  • 40
  • 69
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 !

Bharadwaj
  • 135
  • 1
  • 1
  • 7