I am new to C. So here's my question. Is it consider a bad practice to use negative index when using a pointer?
consider the following code:
#include <stdio.h>
void loopDown(int *intPointer, int maxStep);
int main() {
int myArray [10]={0};
myArray[0] = 5;
int myMaxStep = 5;
int *myPointer = myArray + myMaxStep;
// loopDown(&*myPointer,myMaxStep); //Bad practice suggested by Jonathan
loopDown(myPointer,myMaxStep); //Edited version
return 0;
}
void loopDown(int *intPointer, int maxStep){
int i;
int j;
for(i=0;i<=maxStep;i++){
// printf("%d",i);
j = -1*i;
printf("%d\n",intPointer[j]);
}
};
and it's output:
0
0
0
0
0
5
unlike python(another language that I use), c doesn't have a quick way to slice an array. And I think pointer is a very convenient solution for me to "cut" the array without actually cutting it. Would this be consider hard to read/bad practice or have a better situation to it?
if there is other bad practice in other portion of the code feel free to point it out. I am still learning. Thanks :)