title: Printing a pointer of an 3D array element using pointer arithmetics
body: <p>I am testing how to use array name with pointer arithmetic to access an array elemnets and I have come up with this program: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(){
  
      // definition of array using designators
      int a[2][2][2] = {
          [0] = {[0] = {1, 2}, [1] = {3, 4}},
          [1] = {[0] = {5, 6}, [1] = {7, 8}}
      };
  
      printf("7th  element (pointer):  %p\n", *(*(a + 1) + 1) + 0);
      printf("8th  element (pointer):  %p\n", *(*(a + 1) + 1) + 1);
  
      return 0;
  }
  </code></pre>
  
  <p>Although program works and prints everything correct: </p>
  
  <pre><code>7th  element (pointer):  0x7ffd4b09a1c8
  8th  element (pointer):  0x7ffd4b09a1d0
  </code></pre>
  
  <p>I get warnings at compile time saying something like this for every line where I use <code>%p</code> place holder inside <code>printf()</code>: </p>
  
  <pre><code>warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int *’ [-Wformat=]
    printf("7th  element (pointer):  %p\n", *(*(a + 1) + 1) + 0);
                                     ~^     ~~~~~~~~~~~~~~~~~~~
                                     %ls
  </code></pre>
  
  <p>So at the first glance it looks like I have to simply cast the pointers to <code>(void *)</code> and if I do this and change line:</p>
  
  <pre><code>printf("8th  element (pointer):  %p\n", *(*(a + 1) + 1) + 1);
  </code></pre>
  
  <p>With this line: </p>
  
  <pre><code>printf("8th  element (pointer):  %p\n", (void *)(*(a + 1) + 1) + 1);
  </code></pre>
  
  <p>I get a warning:</p>
  
  <pre><code>warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
    printf("8th  element (pointer):  %p\n", (void *)(*(a + 1) + 1) + 1);
                                                               ^
  </code></pre>
  
  <p>and compiler calculates a different address for 8'th element and this address is only 1 byte larger than preceeding elemnet's address:</p>
  
  <pre><code>7th  element (pointer):  0x7ffd9e5e1bf8
  8th  element (pointer):  0x7ffd9e5e1bf9
  </code></pre>
  
  <p>I also tried to fix it like this <em>(added one more braces)</em>: </p>
  
  <pre><code>printf("8th  element (pointer):  %p\n", (void *)((*(a + 1) + 1) + 1));
  </code></pre>
  
  <p>and warning was gone, but address is still calculated differently: </p>
  
  <pre><code>7th  element (pointer):  0x7ffca6c6c468
  8th  element (pointer):  0x7ffca6c6c470
  </code></pre>
  
  <p>It looks like compiler needs pointer type to calculate the addresses and if I cast it it will not calculate the address corectly. Does anyone have any idea what I can do to remove the warning and get address calculated in a right way?</p>