Note that there are some explanatory texts on larger screens.

plurals
  1. POAre a, &a, *a, a[0], &a[0] and &a[0][0] identical pointers?
    text
    copied!<p>I have the following C program:</p> <pre><code>#include &lt;stdio.h&gt; int main(){ int a[2][2] = {1, 2, 3, 4}; printf("a:%p, &amp;a:%p, *a:%p \n", a, &amp;a, *a); printf("a[0]:%p, &amp;a[0]:%p \n", a[0], &amp;a[0]); printf("&amp;a[0][0]:%p \n", &amp;a[0][0]); return 0; } </code></pre> <p>It gives the following output:</p> <pre><code>a:0028FEAC, &amp;a:0028FEAC, *a:0028FEAC a[0]:0028FEAC, &amp;a[0]:0028FEAC &amp;a[0][0]:0028FEAC </code></pre> <p>I am not able to understand why are <code>&amp;a</code>, <code>a</code>, <code>*a</code> - all identical. The same for <code>a[0]</code>, <code>&amp;a[0]</code> and <code>&amp;a[0][0]</code>.</p> <p><strong>EDIT:</strong></p> <p>Thanks to the answers, I've understood the reason why these values are coming out to be equal. This line from the book by <em>Kernighan &amp; Ritchie</em> turned out to be the key to my question: </p> <pre><code> the name of an array is a synonym for the location of the initial element. </code></pre> <p>So, by this, we get </p> <p><code>a</code> = <code>&amp;a[0]</code>, and</p> <p><code>a[0]</code> = <code>&amp;a[0][0]</code> (considering <code>a</code> as an array of arrays)</p> <p>Intuitively, now the reason is clear behind the output. But, considering how pointers are implemented in C, I can't understand how <code>a</code> and <code>&amp;a</code> are equal. I am assuming that there is a variable <code>a</code> in memory which points to the array(and the starting address of this array-memory-block would be the value of this variable <code>a</code>). </p> <p>But, when we do <code>&amp;a</code>, doesn't that mean taking the address of the memory location where the variable <code>a</code> was stored? Why are these values equal then?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload