0

How to move element in array in order or remove from array?

Example: arr[3,3,2,1]

move(arr[0],arr[sizeof(arr)]);

arr[3,2,1,3];

I wrote function, but is correctly it ?

void remove(int index, char *arr)
{
    arr[index]=NULL;
    for(int i=index;i<sizeof(arr)-1;i++)
        swap(arr[i],arr[i+1]);
}
6
  • Just test the method before asking if it works. Create a bunch of test cases and run them... Commented Oct 25, 2011 at 20:24
  • I can't check with breakpoints, or with "cout<<" in end of function, this functions doesn't works in VS Express 2010. Commented Oct 25, 2011 at 20:26
  • 2
    What is swap? Is it C or C++? Commented Oct 25, 2011 at 20:27
  • You need to provide better examples of what you are trying to achieve, this is quite unclear. Commented Oct 25, 2011 at 20:45
  • swap is standard in C++ (after a using namespace std), but it could also be a C function. Hard to tell. Commented Oct 25, 2011 at 20:45

6 Answers 6

2

Well, first off, I'm going to suggest using std::vector instead of an array since you're using C++. It will make this process much simpler. As for your function, I want to cover a few things...

 arr[index] = NULL;

If I remember right, NULL is 0, so this is fine. However, I'm assuming you're under the impression that this is a pointer, which it is not. Although you may have a pointer to the array, each individual element of the array is not a pointer. To get this, you would need to pass a char **. The reason this works in this case is because a char is really an int.

sizeof(arr)-1

This will not get you the number of elements of the array. You asked for the size of arr, which is going to return the size of the pointer data type. A better option here would be to iterate through with pointer arithmetic until you reach the end of the data. Remember, a pointer to an array by the end of the day is still just a pointer and does not contain any overhead about the array itself. It just knows which element it is pointing to.

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

4 Comments

Are you sure it's C++? I thought so too, but with the errors, it's hard to tell.
There's not much to go by as far as the code goes. The question is however, tagged C++, so once can only assume I guess.
I made some mistakes since the newly acquainted with the language
In his comments on my post, he's getting runtime errors using std::rotate, so C++ seems accurate.
1

This already exists in the C++ standard library (std::rotate), and the C standard library (memmove)

If you're using C++, (since it comes with a std::swap function, and C can't swap with parameters like that):

void remove(int index, char *arr, int len)
{
    if (len<=0)
        len = strlen(arr);
    std::rotate(arr+index, arr+index+1, arr+len);
    arr[len-1] = NULL;
}

Also, in C++, use a std::vector or std::string rather than a naked char* pointer.

If you're using C:

void remove(int index, char *arr, int len)
{
    if (len<=0)
        len = strlen(arr);
    memmove(arr+index, arr+index+1, len-index-1);
    arr[len-1] = NULL;
}

If you are using naked char* pointers, always pass a length.

7 Comments

I just changed the answer. Which isn't what you need?
How to move my element to the end if I'm using char array?Or how to correctly use std::rotate function?
@wsevendays: the remove function in my answer uses std::rotate to move the char to the end, and then it sets that to NULL.
I have an error:invalid iterator range, when using first function."remove(shariki[i],shariki);"
The first parameter to remove is an int index. Is shariki[i] an int or a char? And is the length of your array really 1? You probably want remove(i, shariki, length);
|
1

You don't have to NULL the element or swap.

void remove(int index, char *arr)
{
    int i;
    for(i=index;i<strlen(arr)-1;i++)
        arr[i] = arr[i+1];
    arr[i] = NULL;  // i now points to last, "extra" element
}

5 Comments

@wsevendays This likely does not do what you hope it will do. In particular, sizeof(arr) will be the number of bytes in a pointer. It might be 4 or it might be 8 or something else.
@CaptainGiraffe changed sizeof() to strlen()
I wouldn't put any money on this being a zero terminated string.
Thanks for the "sizeof() to strlen()".
@wsevendays @CaptainGiraffe makes another good point: if you're not NULL terminating this array, you have to use another variable to keep track of its size and then decrement that in your remove() function, and then instead of strlen(arr) just use that variable.
1

The sizeof(arr) when arr is declared as actual array with dimension will give you the size of the array in bytes (actually, C++ does not know bytes, only chars, but lets not get into that right now). The arr[sizeof(arr)] makes no sense: you are telling the compiler to get the element that is at the index numerically equal to the size of array in bytes, which is always out of bounds. BTW, to get the size of array in elements, use: sizeof(arr) / sizeof(arr[0]).

The sizeof(arr) when char *arr will give you the size of the pointer (typically 4 or 8 bytes depending on bitness), regardless of how large the array "beneath" that pointer is.

Even when you fix all that (and couple of other more minor things such as using NULL as integer), you will still be just moving the element to the end of the array, not actually re-moving it.

I suggest you use std::vector and its erase method. If you are doing a lot of removes from the middle (and you can live without random-access), consider using std::list (and its erase) instead. Alternatively, you may also consider std::remove.

1 Comment

Importantly, when you have a function declared void fn(char arr[32]), sizeof arr would be sizeof(char*) rather than 32
0

Do not swap your elements just shift all right allocated elements one position left in order to overwrite you element. Do not forget to decrease size of your array.

Comments

-3

It looks correct, indeed. You should distinguish between cases when you want to remove an element from the array, or simply move it. Perhaps it is a good idea to transfer the count (by how much to move) as a parameter, and when it is equal to the array length (or 0, for example) - the element would be deleted.

1 Comment

The good news is, if you delete your answer you'll get the "peer pressure" badge.

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.