8

As a beginner programmer I am dealing with some simple problems related to Pointers. In the following code I found the value of *a and a are same in hexadecimal. But I can't understand the reason.

#include <stdio.h>
#include <stdlib.h>

main(){
    int a[5][5];
    a[0][0] = 1;

    printf("*a=%p  a=%p \n", *a, a);

    return 0;
}

Here is the output:

*a=0x7ffddb8919f0  a=0x7ffddb8919f0
Jashaszun
  • 9,079
  • 3
  • 24
  • 53
biswas N
  • 381
  • 1
  • 16

3 Answers3

9

An array and its first element have the same address.:)

For this declaration

int a[5][5];

expression a used in the printf call is implicitly converted to the pointer to its first element. Expression *a yields the first element of the array that is in turn a one-dimensional array that also is converted to a pointer to its first element.

Thus expressions a and *a have the same value as expression &a[0][0]

Vlad from Moscow
  • 265,791
  • 20
  • 170
  • 303
5

In C and C++ languages values of array type T [N] are implicitly converted to values of pointer type T * in most contexts (with few exceptions). The resultant pointer points to the first element of the original array (index 0). This phenomenon is informally known as array type decay.

printf argument is one of those contexts when array type decay happens.

A 2D array of type int [5][5] is nothing else than an "1D array of 1D arrays", i.e. it is an array of 5 elements, with each element itself being an array of 5 ints.

The above array type decay rule naturally applies to this situation.

The expression a, which originally has array type int [5][5], decays to a pointer of type int (*)[5]. The pointer points to element a[0], which is the beginning of sub-array a[0] in memory. This is the first pointer you print.

The expression *a is a dereference operator applied to sub-expression a. Sub-expression a in this context behaves in exactly the same way as before: it decays to pointer of type int (*)[5] that points to a[0]. Thus the result of *a is a[0] itself. But a[0] is also an array. It is an array of int[5] type. It is also subject to array type decay. It decays to pointer of type int *, which points to the first element of a[0], i.e. to a[0][0]. This is the second pointer you print.

The reason both pointer values are the same numerically is that the beginning of sub-array a[0] corresponds to the same memory location as element a[0][0].

AnT
  • 302,239
  • 39
  • 506
  • 752
1

a can be considered a pointer to a pointer to an int (in reality, it's an array of array of int, but close enough).

So a and *a both point to the same address (which happens to be a[0][0]).

*a is still a pointer, and a[0] is the same address as a[0][0].

Jashaszun
  • 9,079
  • 3
  • 24
  • 53
  • This is too misleading to be useful. – juanchopanza Aug 11 '15 at 17:08
  • @juanchopanza Excuse me? Could you explain why? – Jashaszun Aug 11 '15 at 17:09
  • Absolurtely incorect answer. `a` cannot be considered "a pointer to a pointer". 2D array in C and C++ is not in any way related to "pointer to a pointer" – AnT Aug 11 '15 at 17:11
  • Saying stuff like "`a` can be considered a pointer to a pointer", or "`*a` is still a pointer" and so on. – juanchopanza Aug 11 '15 at 17:11
  • @juanchopanza And what's wrong with that? – Jashaszun Aug 11 '15 at 17:12
  • Oh, only *everything*. – juanchopanza Aug 11 '15 at 17:12
  • @AnT How so? Please explain. – Jashaszun Aug 11 '15 at 17:12
  • @Jashaszun: I don't see what else needs to be explained here. Array in C and C++ is not a pointer. 2D array in C and C++ is not a pointer to pointer and cannot be meaningfully considered as such. That's just it is in the language. – AnT Aug 11 '15 at 17:21
  • @AnT I don't see how my answer is any different than VladfromMoscow's... I am stating that `a` and `*a` both have addresses equivalent to `&a[0][0]`, just like his. – Jashaszun Aug 11 '15 at 17:23
  • Note that this misconception is so widespread that three people have up-voted your answer. – juanchopanza Aug 11 '15 at 17:23
  • 1
    @juanchopanza Yet even VladfromMoscow says `pointer` in his answer, just like I do, yet isn't downvoted/chastised by you... – Jashaszun Aug 11 '15 at 17:24
  • @Jashaszun: Just because you arrived at the same conclusion does not mean that what you said in your answer is true. The claim that 2D array can be considered "a pointer to a pointer" is nonsensical and misleading. Claiming that a 1D array "can be considered a pointer" is also a stretch, but it can be accepted as a simplification in *some* cases. But for 2D arrays "a pointer to a pointer" analogy is completely and utterly irredeemable. – AnT Aug 11 '15 at 17:27
  • Note, that at no point there's "a pointer to a pointer" involved in VladfromMoscow's answer. – AnT Aug 11 '15 at 17:32
  • @Jashaszun Vlad's answer is fine, yours isn't. Read both carefully and stop whinging. – juanchopanza Aug 11 '15 at 17:34