0

I have to define a function to delete an element in an array, here is the code

void delete_element(int a[], int n, int pos)
{
    if (pos>=n)
    pos=n-1;
    else if (pos<0)
    pos=0;
    for (int i=pos-1;i<n-1;i++)
    {
        a[i]=a[i+1];
    }
    --n;
}

and here is an example:

int n;
printf("Enter the length of the array: ");
scanf("%d", &n);
int A[n]
for (int i=0;i<n;i++)
scanf("%d", &A[i]);
delete_element(A,n,2);

suppose that n=5 and A = {1,2,3,4,5}, after running the above code, it will print out {1,3,4,5,5}

When I use int n as parameter, the function deletes the element I want but the last element will appear twice in the array. I searched and then found out that by using int &n, the problem will be solved but I don't understand the reasons here. I would really appreciate if you could help me with this!

Thien Anh
  • 15
  • 3
  • 1
    This is C++ code, not C. – HolyBlackCat Jan 04 '22 at 08:36
  • Please show a [mre], at a guess the caller of the code doesn't update the size of the array? – Alan Birtles Jan 04 '22 at 08:46
  • try search for "pass by reference pass by value"? – kenash0625 Jan 04 '22 at 08:49
  • This function doesn't delete anything, no matter how `n` is taken. It merely shifts elements in an array. – Evg Jan 04 '22 at 08:51
  • @Evg I watched some vids on youtube and all of them said that the idea for deleting an element is to shift the array, idk how to literally delete it – Thien Anh Jan 04 '22 at 09:07
  • 3
    You can't change the size of a C-style array, so you can't delete elements from it. You can only change their values, and that's what you're doing. To actually delete an element, you have to create a new array of decreased size and copy elements into that new array. However, in C++ you should prefer `std::vector` that gives you a desired semantics of a resizable array. – Evg Jan 04 '22 at 09:15
  • To emulate a resizable C-style array, you could keep its "size" in a separate variable, like `n` in your example. When you decrement `n`, you "delete" an element. But technically, this doesn't count as deletion. – Evg Jan 04 '22 at 09:19

2 Answers2

1

If you use void delete_element(int a[], int n, int pos), the arguments are copied to the function. So if you decrease n by using --n;, it will only affect the "local" n, the copy.

If you use void delete_element(int a[], int& n, int pos), the parameter n is padded by referece. This means, there is no local copy used in the function but the variable n from the "outside world".
Therefore, --n; will now affect the variable which is given as a parameter to your function. You could create the same behavior by passing a pointer to the variable. In that case, the address is copied, but instead of the copied address the memory location it points to, the original variable, will be modified.

Dennis
  • 150
  • 1
  • 11
  • thank you so much, I understood it finally. – Thien Anh Jan 04 '22 at 09:22
  • 1
    Please avoid posting links to gfg. This site is a collection of C++ code examples of extremely bad quality and it better be avoided by beginners. – Evg Jan 04 '22 at 09:24
  • oh ok, thanks @Evg. What would be better? – Dennis Jan 04 '22 at 12:46
  • Typically we refer to introductory books in [this list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Evg Jan 04 '22 at 12:48
0
  1. This function doesn't delete an element in an array actually, because it just overlaps the data at pos with the next data, and the size is not changed.
  2. It seems that n is array's size, so when you use int n, the size is passed as value, so outer n is not changed. And when you use int& n, the size is passed as reference, so n will be changed.
  3. If you want to delete an element in array actually, you may refer to pop_back() or pop_front() function of vector.
Uuq114
  • 16
  • 2
  • Thank you so much, I figured it out. But that means if I want to change the value of n inside a function, I have to use int &n instead of int n, did I get it right? – Thien Anh Jan 04 '22 at 09:12
  • @ThienAnh Yes. In most cases, reference is faster than value, so you can also use `const int& n` if you don't want to change value. – Uuq114 Jan 04 '22 at 09:20
  • 1
    References are *not* faster in general and primitive types like `int` should almost *never* be passed by const-ref. – Evg Jan 04 '22 at 09:26
  • @Evg yeah, refs are more useful for more complex types, thanks for supplement – Uuq114 Jan 06 '22 at 06:55