Created
May 23, 2014 14:46
-
-
Save 17twenty/5a8a3de52b6450c0f56c to your computer and use it in GitHub Desktop.
Learn something new about passing arrays, and how awful the syntax can be :D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc -std=c99 due to loop initialiser - stupid GCC defaulting to C89 :( */ | |
#include <stdio.h> | |
#define ARRAY_SIZE(x) \ | |
((sizeof(x) / sizeof(x[0]))) | |
void count_and_process_items(unsigned int (*array)[10]) | |
{ | |
for (int i = 0; i < ARRAY_SIZE(*array); ++i) { | |
printf("Item %d = %d\n", i, (*array)[i]); | |
printf("\twhich should also be %d\n", (i)[*array]); | |
} | |
} | |
int main(int numArgs, char **args) | |
{ | |
unsigned int foo[] = { | |
1,2,3,4,5,6,7,8,9,0 | |
}; | |
count_and_process_items(&foo); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice thing about this is that it makes the compiler do the checking of element numbers for you and ARRAY_SIZE still works as it can normally break (honestly!)