Skip to main content
GameDev.net gamedev.net
🔒 Locked

Arrays are EVIL

Started by daviangel Apr 3, 2008 at 1:45 AM 5 replies 2.2k views
Original Post
daviangel
daviangel
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!
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
LackOfGrace
LackOfGrace
arrays arent evil
uncommented badly-written code is evil :)
Antheus
Antheus
There's absolutely nothing evil about the above code.

It merely demonstrates multi-dimensional array memory layout, pointer arithmetic and pointer dereferencing.
DeadXorAlive
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 :)
ToohrVyk
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]

DeadXorAlive
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.
MaulingMonkey
MaulingMonkey
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 ;-)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.