#include<stdio.h>
int main(){
int i,j;
int ans[5][5];
ans[0][0] = 10;
printf(" why this %d \n\n",ans[0,0] );
}
and output is some garbage value i tried this in codeblocks
You declared a two-dimensional array
int ans[5][5];
The element type of the array is int[5]
. So for example the expression ans[0]
gives such an array. Arrays used in expressions (with rare exceptions) are converted to pointers to their first elements. So the expression ans[0]
having the type int[5]
is implicitly converted to pointer of the type int *
to the element ans[0][0]
In this expression
ans[0,0]
in the square brackets there is used an expression with the comma operator 0,0
. As the first operand of the expression with the comma operator does not have a side effect then the above expression is equivalent to ans[0]
.
That means that in this call
printf(" why this %d \n\n",ans[0,0] );
that is equivalent to
printf(" why this %d \n\n",ans[0] );
you are trying to output a pointer using the conversion specifier %d
designed to output objects of the type int
that results in undefined behavior.
It seems you mean
printf(" why this %d \n\n",ans[0][0] );
or
printf(" why this %d \n\n", **ans );
or
printf(" why this %d \n\n", *ans[0] );
or
printf(" why this %d \n\n", ( *ans )[0] );
As for the comma operator then just consider the following code snippet
int i = 0;
ans[0][0] = ( ++i, ++i, ++i, ++i, ++i, ++i, ++i, ++i, ++i, ++i );
the result of which is equivalent to the following code snippet
int i = 10;
ans[0][0] = i;