Browse Source Download (without any required ccan dependencies)
array_size
routine for safely deriving the size of a visible array.
Rusty Russell <rusty@rustcorp.com.au>
This provides a simple ARRAY_SIZE() macro, which (given a good compiler) will also break compile if you try to use it on a pointer.
This can ensure your code is robust to changes, without needing a gratuitous macro or constant.
// Outputs "Initialized 32 values"
#include <ccan/array_size/array_size.h>
#include <stdlib.h>
#include <stdio.h>
// We currently use 32 random values.
static unsigned int vals[32];
int main(void)
{
unsigned int i;
for (i = 0; i < ARRAY_SIZE(vals); i++)
vals[i] = random();
printf("Initialized %u values\n", i);
return 0;
}
CC0 (Public domain)