-13

What is going on here in this line:

-1[0[ptr]][0] 
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    You can't post some random, totally out of context characters of (presumably) code here and expect us to explain it to you. Without other code that gives that any chance of having meaning, it can't be explained. Also, *can you tell me what is going on here?* is not any sort of meaningful title or question. You'll find your experiences here will be much better if you spend some time taking the [tour] and reading the [help] pages (especially [ask]) to learn how the site works and what our expectations are before you begin posting. – Ken White Dec 18 '19 at 00:44
  • I would say, that would result in a compilation error or a SEGENV(Segmentation fault) – dan1st Dec 18 '19 at 05:46
  • @dan1st Nope. See my answer below. it is valid C/C++ – robthebloke Dec 18 '19 at 06:34
  • @robthebloke ok... – dan1st Dec 18 '19 at 06:38
  • @dan1st Admittedly it's not a coding style I'd recommend :) – robthebloke Dec 18 '19 at 06:38

1 Answers1

1

Technically speaking, that is valid C code, but it is not worth writing it like that. Consider this stupid example.

char v = array[3];

Now, we can rewrite that in pointer arithmetic. e.g.

char v = *(array + 3);

However, since addition is commutative, it follows that this is also valid:

char v = *(3 + array);

And since addition is commutative, it therefore follows that array brackets are also commutative. So this is perfectly valid in C:

char v = 3[array];

If you wish to untangle the code you've posted, you can do it in steps....

float f0(float*** ptr)
{
    return -1[0[ptr]][0];
}
float f1(float*** ptr)
{
    return -1[ptr[0]][0];
}
float f2(float*** ptr)
{
    return -ptr[0][1][0];
}

And here is the proof they are the same:

https://godbolt.org/z/_gSUXF

(f2 and f3 just result in tail calls to f1)

/edit I think it's a little unfair you've been marked down so harshly for asking this question. It's actually a cool quirk of C/C++ that very few people know.

robthebloke
  • 9,331
  • 9
  • 12