Reading between the lines, Question 4.13: What's the total generic pointer type? in the C FAQ seems tangentially related:
There is no "total generic pointer type.".
The declaration void *A[] indicates that the function is expecting a list of void pointers, not a generic pointer to data elements. The kind of interface you seem to be seeking to implement can be found in the standard qsort function.
void qsort(void *base, size_t nel, size_t width,
int (*compar)(const void *, const void *));
Note base points to the first element of the array to be sorted. Note also that the second and third arguments are essential for qsort to be able to to different elements of the array when invoking the comparison function compar. We assume compar knows what data type its arguments are supposed to be pointing to.
I recommend studying both the prototype of qsort and compar so that you can better frame the design you are after.
While the example below probably does not correspond directly to what you are trying to do, it should be decent starting point for brainstorming:
#include <stdio.h>
#include <stdlib.h>
enum item_type {
INTARR,
INTARRLIST,
DOUBLEARR,
DOUBLEARRLIST,
};
struct item;
struct item {
enum item_type type;
size_t nelem;
void *item;
};
void
print_int_arr(const int *x, size_t nelem)
{
for (size_t i = 0; i < nelem; ++i)
{
printf("%d\n", x[i]);
}
}
void
print_int_arr_list(const struct item *x[], size_t nelem)
{
for (size_t i = 0; i < nelem; ++i)
{
const struct item *y = x[i];
print_int_arr(y->item, y->nelem);
}
}
void
print_item(struct item *x)
{
switch(x->type)
{
case INTARR:
print_int_arr(x->item, x->nelem);
break;
case INTARRLIST:
print_int_arr_list(x->item, x->nelem);
break;
default:
fprintf(stderr, "Unknown item type '%d'\n", x->type);
exit(EXIT_FAILURE);
}
}
int
main(void)
{
int x[] = {1, 3, 5, 7, 9};
struct item a = { INTARR, 5, x };
struct item b = { INTARR, 3, x };
struct item c = { INTARR, 1, x };
struct item *d[] = { &a, &b, &c };
struct item e = { INTARRLIST, 3, d };
print_item(&a);
print_item(&e);
return EXIT_SUCCESS;
}
void *Ainstead ofvoid *A[]