c - How can this be dereferencing ‘void *’ when the pointer was declared with a type? -
i'm trying make array file-level or global scope size determined @ runtime. various articles this one suggest pattern such dynamic array:
static misctype *handles; static int nthreads; int main(int argc, char **argv) { // nthreads set from argv handles = malloc(nthreads * sizeof(misctype)); for(i = 0; < nthreads; i++) { handles[i] = miscfunction(); ... free(handles); return 0; } int otherfunction(...) { for(i = 0; < nthreads; i++) { handles[i] = miscfunction(); ... }
however, everywhere use pointer array (i.e. handles[i]
) compiler says warning: dereferencing ‘void *’ pointer
, error: invalid use of void expression
.
but it's not void pointer! has type (misctype in pseudocode), , re-casting same type doesn't stop errors. pass void *
or that.
what's going on here? more importantly, can it?
okay, start with, if compiler tells handles
void *
, should believe it. now, turns out malloc
returns void *
, deduce handles
still void *
after assignment.
from may deduce assignment isn't doing think is. @ point have ask "so misctype
", , we, using code you've posted, can't deduce that. if grok misctype
is, bet you'll find answer.
Comments
Post a Comment