• Advertisement
Sign in to follow this  

Arrays are EVIL

This topic is 3609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I guess I never really understood how bad they were until I came across this evil looking code brushing up on my C:
/* 
   pointers and multidimensional arrays
*/

#include <stdio.h>

int main(void)
{
  int zippo[4][2] = { {2,4}, {6,8}, {1,3}, {5, 7} };

  printf("  zippo = %p, zippo + 1 = %p\n",
            zippo, zippo + 1);
  printf("zippo[0] = %p, zippo[0] + 1 = %p\n",
          zippo[0],  zippo[0] + 1);
  printf(" *zippo = %p, *zippo + 1 = %p\n",
           *zippo,  *zippo + 1);
  printf("zippo[0][0] = %d\n", zippo[0][0]);
  printf(" *zippo[0] = %d\n", *zippo[0]);
  printf("  **zippo = %d\n", **zippo);
  printf("    zippo[2][1] = %d\n", zippo[2][1]);
  printf("*(*(zippo+2) + 1) = %d\n", *(*(zippo+2) + 1));
  printf("*(zippo[2] + 1) = %d\n", *(zippo[2] + 1));

  return 0;
}



I guess I never did have a really good understanding of arrays in the first place since it took me quite a while to decipher this evilness too!

Share this post


Link to post
Share on other sites
Advertisement
There's absolutely nothing evil about the above code.

It merely demonstrates multi-dimensional array memory layout, pointer arithmetic and pointer dereferencing.

Share this post


Link to post
Share on other sites
Well it helps if you realize that arrays are little more than pointers to blocks of memory, then this code isn't so evil anymore :)

Share this post


Link to post
Share on other sites
Quote:
Original post by DeadXorAlive
Well it helps if you realize that arrays are little more than pointers to blocks of memory, then this code isn't so evil anymore :)


There, corrected your post for you [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by DeadXorAlive
Well it helps if you realize that arrays are little more than pointers to blocks of memory, then this code isn't so evil anymore :)


There, corrected your post for you [smile]


Oh ok thanks. I was typing this with the 'arrays decay to pointers' thing in mind.

Share this post


Link to post
Share on other sites
Quote:
Original post by DeadXorAlive
Quote:
Original post by ToohrVyk
Quote:
Original post by DeadXorAlive
Well it helps if you realize that arrays are little more than pointers to blocks of memory, then this code isn't so evil anymore :)


There, corrected your post for you [smile]


Oh ok thanks. I was typing this with the 'arrays decay to pointers' thing in mind.


The decay part is important -- when people forget this, they do things like cast a int[4][2]* to an int*** and expect it to work.
(void*)array   == (void*)&array, typically
(void*)pointer != (void*)&pointer, typically

None of this was touched upon in the relatively tame originally posted snippet ;-)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement