Skip to content

Instantly share code, notes, and snippets.

@17twenty
Created May 23, 2014 14:46
Show Gist options
  • Save 17twenty/5a8a3de52b6450c0f56c to your computer and use it in GitHub Desktop.
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
/* 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;
}
@17twenty
Copy link
Author

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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment