# Arrays are EVIL

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

## 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 on other sites
arrays arent evil
uncommented badly-written code is evil :)

##### Share on other sites
There's absolutely nothing evil about the above code.

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

##### 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 on other sites
Quote:
 Original post by DeadXorAliveWell 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 on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by DeadXorAliveWell 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 on other sites
Quote:
Original post by DeadXorAlive
Quote:
Original post by ToohrVyk
Quote:
 Original post by DeadXorAliveWell 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 ;-)

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633360
• Total Posts
3011524
• ### Who's Online (See full list)

There are no registered users currently online

×