1

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 :)

  • The use of `&*` in `loopDown(&*myPointer,myMaxStep);` is considered bad practice. – Jonathan Leffler Apr 15 '22 at 20:39
  • 1
    It's unusual to use negative indexes, but when you've set things up so that you have a pointer to the end of an array and index backwards from that, then negative indexes are OK. – Jonathan Leffler Apr 15 '22 at 20:40
  • @JonathanLeffler thanks:) any idea how you would change it? – schrodinger_s_c_language Apr 15 '22 at 20:42
  • Assuming you're referring to the `&*`, then remove both symbols; `loopDown(myPointer, myMaxStep);` should work the same as what you've written, but without the obfuscatory `&*`. – Jonathan Leffler Apr 15 '22 at 20:43
  • got it! yes i tried it and it works! Thanks for helping my code less confusing – schrodinger_s_c_language Apr 15 '22 at 20:45
  • You might look at [`calloc()` for an array of array with negative index in C?](https://stackoverflow.com/q/26350717/15168) It may not help all that much, and/or it may be more obfuscated than just using `&*` is. – Jonathan Leffler Apr 15 '22 at 20:50
  • okay I see, using calloc() function I would have more flexibility in my array instead of implementing much more complex sollution – schrodinger_s_c_language Apr 15 '22 at 20:58
  • 2
    I am not sure whether I would call this solution "bad practice", but it is unusual and thus may confuse readers of the code. It can easily be rewritten to avoid negative indices and I think that would be easier to understand for most C developers, so I do not see any reason to use negative indices in this case. – nielsen Apr 15 '22 at 21:30
  • 1
    The alternative is `void loopDown(int *intPointer, int last, int first)` where `intPointer` points to the first element of the array, and `last` and `first` are indexes into the array. Then the loop is `for (int i = last; i >= first; i--)`. – user3386109 Apr 15 '22 at 22:47
  • 1
    The big difference between Python and C is that when you index outside of a list in Python, an exception is thrown, and the program terminates (unless you handle the exception, of course). When you index outside of an array in C, [bad stuff happens](https://en.wikipedia.org/wiki/Undefined_behavior). So playing games with pointers, and negative indexes is indeed very bad practice. – user3386109 Apr 15 '22 at 23:02
  • At the end of the day, it all comes as an arithmetic sum of the index times the size of the element added to the address of the array, and in your case the result is always within range. It's no more dangerous than using positive values, it's just unusual and harder to maintain (for others). – Déjà vu Apr 16 '22 at 00:28
  • placing a `;` after the closing brace at the end of a function is not part of the C language. Suggest removing that `;` – user3629249 Apr 16 '22 at 20:08
  • thank you everyone for responding to my questions. So from what i gathered up, it is okay to use negative indices for an array however it is better to have good explanation/comment with it so it won't be difficult to use or understand. The take away is probably human is prone to error and so my code need to design to be more human friendly. – schrodinger_s_c_language Apr 16 '22 at 20:27

1 Answers1

1

The computer does not care. The compiler does not care. The C standard defines semantics for negative offsets from pointers. Programs using them correctly will work as desired.

So the only consideration is humans. Humans are trouble. They misunderstand things, they make mistakes. They develop habits, they make assumptions. Some of them may assume “array indices” are nonnegative, even though nothing in the C standard says so.

It is fine to use negative pointers, but make sure the error-prone humans know what you are doing. Sometimes negative indices arise naturally. There are signal filters that are compute the next new output as a function of some inputs and a few previous outputs. In this case, when OutputPointer is pointing to the place where the next output should be stored, the previous outputs are naturally OutputPointer[-1], OutputPointer[-2], and so on.

All source code should be well documented, regardless of whether it uses negative offsets or not. Make it clear to the reader what the software is doing and why.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312