0

while reading about the c preprocessor I got something like array[x=y,x+1]. I haven't seen this kind of syntax in c before and after searching for many hours I didn't find anything useful.

#include <stdio.h>

int main() {
    int arr[] = {5,10,15};
    printf("%d %d %d",arr[0,1]);
    return 0;
}

outputs: 10 1762365112 1769491896

Can someone elaborate on this?

coco
  • 19
  • 6

1 Answers1

2

Because of how the comma operator (,) works in C, the effect of the (questionable and woefully hard to read) array[x=y,x+1] is to first copy the value of y into x and then use x+1 as the normal (single dimensional, non-ranged, or whatever you were expecting) index in to the array.

(I do not see the need to discuss your experimental code.)

See the link to documentation of comma operator, as kindly provided in comments by Pignotto:
https://en.cppreference.com/w/c/language/operator_other#Comma_operator

Yunnosch
  • 26,130
  • 9
  • 42
  • 54