user
user

Reputation: 33

How to delete element from array by pointers

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.

Upvotes: 0

Views: 1399

Answers (2)

Bharadwaj
Bharadwaj

Reputation: 145

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 !

Upvotes: 0

Acorn
Acorn

Reputation: 26066

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.

Upvotes: 1

Related Questions