The main problem is that you've declared LOOKUP
to be an array of pointers to int
, not an array of arrays of int
, so the type of LOOKUP[0]
is going to be int *
. The secondary problem is that, most of the time, array expressions will be converted to pointer expressions and the array size information will be lost.
If you need to know the size of an array in any context where the array expression has already been converted into a pointer expression, you need to store that size separately.
Here's one approach - use a struct to associate the array pointer with the array size:
#include <stdio.h>
int main(void)
{
int NUMBERS1[] = {1, 2, 3, 4, 5};
int NUMBERS2[] = {1, 2, 3, 4, 5, 6};
int NUMBERS3[] = {1, 2, 3, 4, 5, 6, 7};
struct arr_and_size {
int *arrp;
size_t size;
};
struct arr_and_size LOOKUP[] = {{NUMBERS1, sizeof NUMBERS1},
{NUMBERS2, sizeof NUMBERS2},
{NUMBERS3, sizeof NUMBERS3}};
size_t i = 0;
for (i = 0; i < sizeof LOOKUP / sizeof *LOOKUP; i++)
printf("LOOKUP[%zu].arrp = %p, LOOKUP[%zu].size = %zu\n",
i, LOOKUP[i].arrp, i, LOOKUP[i].size);
return 0;
}