Two students A and B were given an assignment to write a C code in which an
int
array
a
of 5 elements is declared/initialized with
{1,2,3,4,5}
at compile-time and the 2nd element (0-indexing) of
a
is printed in output (stdout).
Given below are the code snippets written by A and B respectively.
Code by A:
1 2 3 4 5 6 |
|
Code by B:
1 2 3 4 5 6 |
|
Which of the two code snippets are correct (they give the desired output without any errors) ?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
In C, both a[2] and 2[a] are the same... This has relation to the address of the array(It references to a particular position of the continuous memory allocation) and its pointer variable