You cannot do this with the specific interface you represent, but you can do it somewhat awkwardly:
bool find_null (const void *array, size_t num_ptrs, size_t ptr_size, const void *null) { const char (*ptr_array)[ptr_size] = array; size_t i; for (i = 0; i < num_ptrs; ++i) { if (!memcmp(array[i], null, ptr_size)) return true; } return false; }
You would call it that:
struct Foo; #define ARRAY_SIZE 53 int main(void) { struct Foo *my_array[ARRAY_SIZE] = { ... }; struct Foo * const foo_null = (struct Foo *) 0; if (find_null(my_array, ARRAY_SIZE, sizeof(*my_array), &foo_null)) { puts("It contains NULL"); } else { puts("It does not contain NULL"); } }
Note that this assumes that there is only one representation for the null pointers of the type in question, which is true in many implementations, but not required by the language.
Please also note that this actually has nothing to do with finding null pointers, so you can actually use it to search your array of pointers for any pointer value. In fact, it is not even specific to pointer arrays - you can use it to search for any array for any value if byte-for-byte is equal to a suitable matching criterion (which it is not for structures or unions and may not be for some other types).
Also, if this suits you, you can probably develop a macro wrapper that makes it easier to use some of your more common scripts.
source share