-6

Here is the code i try to run:

void main(){
    int arr[2][3]={0};
    arr[1][2] = 2;
    printf("%d", arr[1, 2]);
}

It gives me the output as 2. What the hell is going on here. I couldn't find an answer on google so i posted it here.

  • You're lucky it didn't CRASH. This is a great example of [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) – paulsm4 Jun 04 '21 at 03:53
  • I closed this as a duplicate. If instead you are asking why `arr[1,2]` which is the same as `arr[2]` provided a result of 2... well then that's just different undefined behavior. But it still just terrible. – Bill Lynch Jun 04 '21 at 03:55
  • First of all you're going out of bounds. Secondly `arr[1, 2]` is the same as `arr[2]`, which your compiler should complain about when used as a source for `%d` in `printf`. – Some programmer dude Jun 04 '21 at 03:56
  • look at the question now. I wrote it wrong , it is not out of bounds. – Shoaib Wani Jun 04 '21 at 04:04
  • So the problem is your use of [the comma operator](https://en.cppreference.com/w/c/language/operator_other#Comma_operator) in `arr[1, 2]`. Which as mentioned is the same as `arr[2]`. Which is an *array* which decays to a pointer. Which can't be printed with `%d`. Mismatching `printf` format specifier and argument type is also undefined behavior. – Some programmer dude Jun 04 '21 at 04:07
  • 2
    If you're not compiling with warnings turned on, fix that at once. `-Wall -Wextra` is a good start for GCC and clang. – Shawn Jun 04 '21 at 04:14
  • Modern compilers can show a diagnostic for this code, if you did not see a warning or error then adjust your compiler settings (and/or upgrade compiler) – M.M Jun 04 '21 at 04:49
  • 1
    @ShoaibWani When a compiler emits an error message such as "out-of-bounds reference to `arr`", and you can't understand what it means - you've almost certainly missed something. In this case, you missed an error where instead of `arr[1][2]` you wrote `arr[1,2]`. Those type of errors are extremely difficult for you to see - when you wrote it, you assumed it was correct so when you see it again your brain still implicitly assumes it's correct code even though it's not. Others will see errors like that immediately. If you keep writing code, get used to that - this won't be the last time... – Andrew Henle Jun 04 '21 at 06:54
  • It is highly unlikely that the output of this code is `2`. Are you sure the code you run is the code you show here? – Jabberwocky Jun 04 '21 at 07:17

1 Answers1

1

array is not out of bound

Correct.

The program has undefined behavior for another reason.

I assume you are confused about this part: arr[1, 2]

The 1,2 is comma seperated expressions, i.e. 1 and 2. The rule of the comma operator is that the result is the result of of the right operand. So 1,2 ends up being 2. Therefore your code is equivalent to:

int arr[2][3]={0};
arr[1][2] = 2;
printf("%d", arr[2]);

This code has undefined behavior due to the printf statement. You are using %d which means that the argument type must be int. However, arr[2] is a pointer. So there is a mismatch between the format specifier and the argument provided. That is undefined behavior.

The correct way to print the pointer is:

printf("%p", (void*)arr[2]);

It gives me the output as 2.

Due to undefined behavior anything may happen.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • *array is not out of bound* Yes, it is. Given `int arr[2][3]={0};`, `arr[1, 2]` refers to `arr[2]`, which is out-of-bounds as only `arr[0]` and `arr[1]` are valid in-bounds references. The fact that there is additional UB does not make that array reference proper. – Andrew Henle Jun 04 '21 at 06:47
  • @AndrewHenle Isn't `arr[2]` one past the array? And thereby a legal pointer as long as no dereference is made? In other words - there is no out-of-bounds access and there is no illegal pointer. – Support Ukraine Jun 04 '21 at 13:12