Take a look at line 7 and 9: One assignment overrides the other.
Line 11: It will be out of bounds. See line 13 for how to correct it.
Line 27: The same problem as line 11.
Line 28: What is the value of ptr at that time? I.e. is it at the start position or not?
Line 31: Basically the same as line 27, but at least it will not go out of bounds.
Line 33: Will also be out of bounds.