Assuming there is already something in the array. It should cycle through the for loop, copy the element into the adjacent spot, then write over the spot directly infront of position..
Need I say it again? Get rid of that *** +1!!!
Here's what's happening. You get an array like this:
Code:
position = 3
element_count = 4
elementArray = {1, 2, 3, 4, x, x, ...}
You shift 4 over, since it is the only element >position.
Code:
elementArray = {1, 2, 3, 4, 4, x, ...}
And then you assign to elementArray[position+1].
Code:
elementArray = {1, 2, 3, 4, 123, x, ...
While that works, I'm sure you intended to have
Code:
elementArray = {1, 2, 3, 123, 4, x, ...
which is what you get without that +1.
Just so you know, if you access uninitialized memory or whatever, you're not guaranteed to get a segmentation fault. (Though on Linux your chances of getting a seg fault are very high.) So just because it runs doesn't mean that it doesn't access uninitialized memory.
Segmentation faults are good, by the way. At least they tell you that something's wrong. There's nothing worse than discovering a bug in code that you thought was solid, tested code . . . and then you think "Why on earth was that code working at all???" and then you have to modify tons of code because all of your code was assigning 1 to position instead of 0 . . .