-2

if my two dimensional array is int array[4][5]= {1,2,3,4,5,6,7,8,9,0,11,12,13,14,15,16,17,18,19,20};

when I print a position: printf("%d\n",array[2,0]);. this is printing the address of the value without giving the &. but if I print like this: printf("%d\n",&array[2][0]); it is printing address of the value. As I have given the & it prints the address. but both the address are different. but if I print printf(",:%d\n",array[3,0]); it is printing the same address of array[2,0].

what is the difference between [2][0] and [2,0] accessing the array elements.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
sharon
  • 734
  • 6
  • 15
  • by seeing the usage of `printf()` it looks more like a `C` question. so why tag `C++`? Though, the context is valid in both the cases. – Natasha Dutta Dec 04 '14 at 12:09

7 Answers7

6

Read about comma operator. array[2][0] is accessing element at third row, first column. array[2,0] is first executing operation (2,0) (which returns 0) and thus is equivalent to array[0], which returns the first row of array (actually &array[0][0] because it decays to a pointer).

Noel
  • 730
  • 6
  • 15
4

Using array[1,2] is just an example of the comma operator being misused -> 1, 2 evaluates both operands and yields 2 -> you are accessing array[2].


Side note: array is not a good name for an array given std::array.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 1
    I fail to see how `std::array` could clash with this name (short of universally discouraged `using namespace std;`). – Griwes Dec 04 '14 at 11:54
  • @Griwes Why tempt fate? Plus, "array" doesn't say anything about the **purpose** of the object. It is always good to name variables according to their meaning, not according to their internal structure. – cnicutar Dec 04 '14 at 11:56
  • Sure, the name is poor, but sometimes "array" is just what it is (I use names like that if I need such an array to init another object that must, say, be looped over to init, for example). – Griwes Dec 04 '14 at 11:58
2

2, 0 invokes the so-called comma operator which discards the value of all expressions which occur before the last comma and represents the value of the expression on the very right.
Hence array[2, 0] is equivalent to array[0], and the array-to-pointer decay yields the address of the element at position 0 in array[0] - that is equivalent to &array[0][0].

Columbo
  • 60,038
  • 8
  • 155
  • 203
1

array[2,0] doesn't do what you think it should. It only has a single index, which is the expression 2,0. That is, it's equivalent to array[(2,0)]. This expression involves the comma operator, which evaluates each of its operands, and the result of which is the result of the last operand. So in your case, it's equivalent to array[0]. This denotes the first of your "inner" arrays, which when you try to print it, undergoes array-to-pointer conversion and gives you a pointer to the start of that array.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
1

Compiler is treating arr[3,0] as arr[0]. This is how comma operator works.

And that's why arr[3,0] and arr[2,0] both are printing the same address because both would resolve to base address i.e arr[0].

ravi
  • 10,994
  • 1
  • 18
  • 36
1

what is the difference between array[1][2] and array[1,2]?

The difference is a pretty big thing.

the first example is an multidimensional array access.

you are accessing the 2nd's elements value of the first array.

The second case is just a 1 dimensional array, where the index is invoked with a comma operator within. This would be, the cpu has to take the 1, discard all operations amde with it when encoutnering the , and after that taking the 2.

So the second case just accesses the 2nd elements value of a 1 dimensional array.

For better udnerstanding you should read this:

What does the comma operator , do?

Community
  • 1
  • 1
dhein
  • 6,431
  • 4
  • 42
  • 74
0

For better readability, you can rewrite your array definition as

array[4][5]= {
               1,2,3,4,5,             //a[0][0~4]
                6,7,8,9,10,            //a[1][0~4]
                11,12,13,14,15,        //a[2][0~4]
                16,17,18,19,20};       //a[3][0~4]
  • array[2,0] is a single-level dereferencing using a single index, having a , comma operator.
  • array[2][0] is a double-level dereferencing, using two indexes.

As per the definition of the , operator,

the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).

Now, saying printf("%d\n",array[2,0]);. get evaluated as printf("%d\n",array[0]);

Similarly, printf("%d\n",array[3,0]); also gets evaluated to printf("%d\n",array[0]);

So, they end up producing same result.

On the other hand, printf("%d\n",&array[2][0]);, refers to the address of 1st element in the 3rd row. So it's different.

If you notice the data type, you'll find the difference easily.

  • array[0] represents the data type of int[5]
  • array[2][0] represents teh data type of int

Note: To print a pointer, you need to use %p format specifier, not %d.

Natasha Dutta
  • 3,242
  • 21
  • 23