[Solved] Array malfunction in C – array is not out of bound [closed]


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.

2

solved Array malfunction in C – array is not out of bound [closed]