On 22 Jan 2018 13:24:55 GMT, arnuld <sun...@invalid.address> wrote:
>AIM: to print elements of a multidimensional array.
>PROBLEM: it prints addresses
>
>I known from C.7.2 of Stroustrup 3/e that there are no multidimensional
>arrays on hardware level. These dimensions exist in compiler-source only.
>Question is a[i] generally prints the value at \i\. Then why not here ?
This faulty assumption is at the root of your confusion. When an
expression is evaluated as the operand of an operator, the result is a
value that has a type. Both value and type can be different for
different operators.
Consider the case
char c[4] = {5,9,18,3};
char *p = c;
size_t t = sizeof c;
char (*q)[4] = &c;
c has type array of four char. p has type pointer to char. t has
type size_t which is an alias for some unsigned integer type. q has
type pointer to array of four char.
For purposes of discussion, assume c occupies the four bytes of memory
starting at 0x1000.
c is the right operand of the binary operator =. In evaluating the
expression, the compiler uses the general rule that an expression with
type of array of T is converted to an expression of type pointer to T
with the value address of the first array element. Thus p is assigned
the value 0x1000 with type char*.
c is the operand of the unary operator sizeof. In evaluating the
expression, the compiler uses the exception to the general rule and
does not perform the conversion. Thus t is assigned the value 4 with
type size_t.
c is the operand of the unary operator &. This also is an exception
to the general rule. No conversion is performed and q is assigned
address of the array, value 0x1000 but this time with type (char*)[4].
Note that p and q are not equal. They both point to the same spot in
memory but they need not be the same size or even have the same
internal bit representation.
In your code, arr has type array of four array of four int. Therefore,
arr[0] is the first array (of four int) in that array of four arrays.
arr[1] is the second such array. arr[2][0] is the first int in the
third array of four int. arr[3][2] is the third int in the fourth
array of four int.
When used in an expression of the form
cout << x ....
the << operator is massively overloaded for the various types its
operands can have.
When you code arr[i][j], the compiler will select the overload
version that corresponds to int and you see the value you are
expecting.
When you code arr[i] or arr[j], those expressions have type array
of int and are not exceptions to the general rule. Thus, the
expression is converted and the compiler selects the overload version
that corresponds to int*
Except for char*, the overloaded versions of the << operator that deal
with pointers result in the stream containing the address that was
evaluated (similar to %p in printf) and the contents at that address
are ignored. (The peculiar case of char* is used to insert strings,
as C uses that term, and is similar to %s in printf and the contents
at the address are sent to the stream.)
Remove del for email