By doing
memset(lookup, 0, 100 * sizeof *lookup);
you do not "initialize a pointer" but the memory the pointer lookup
points to.
And most probably you did not made the pointer point to some valid memory, prior to calling memset()
, so writing to random memory invokes undefined behaviour, which crashes the program.
There are several way to get valid memory.
Get it from the stack like so:
bool lookup_instance[100] = {0};
bool (*lookup)[100] = &lookup_instance;
/* No need to memset it to 0s as this had been done by ... = {0} already. */
or get it yourself like so:
bool (*lookup)[100] = malloc(sizeof *lookup);
if (NULL == lookup)
{
perror("malloc() failed"):
}
else
{
memset(lookup, 0, sizeof *lookup);
}
The shortest solution would be:
bool (*lookup)[100] = calloc(1, sizeof *lookup);
if (NULL == lookup)
{
perror("calloc() failed");
}
else
{
/* No need to memset it to 0s as this has been done by calloc() already. */
}