Function for moving string in an array

--
Last edited on
Strings are basically char pointers, a pointer being the address of the "data" to be referenced.

That being said, I think it would be easier to just make an array of pointers and then reference the strings based on that array.

You don't need to physically move the strings data in memory to change how they are read. Just rearrange the order of the pointers in your array.
There are several problems which I can spot immediately.

in line 4, you access your array at loc without first checking if loc is within bounds. If loc is invalid, it could cause your program to crash.

in line 9, the first time the for loop is called, it will try to move the string at position 0 to position -1, which is also a bounds violation.

You should rewrite the function to do the bounds checking first. Also, you can simplify the function such that you only need one for loop. You want to loop from the position after loc to the end of the array.
--
Last edited on
Topic archived. No new replies allowed.