j.j.fishbat@[EMAIL PROTECTED]
writes:
> Is there really no way at all to modify a generic
> pointer by passing a reference to it to a function?
Two people have said "no" here and that is because they have, quote
reasonably, taken this phrase in the context of your previous example.
However, I think you can do what you ask, provided you stick to the
narrowest interpretation of the quote above. (If not, I will get shot
down by the experts and we will both be wiser.)
What you can't do is leave the safety guarantees provided by the void
* type. So, you /can/ do this (example only, I don't recommend it!):
int my_realloc(void **ptr, size_t sz)
{
void *newp = realloc(*ptr, sz);
if (newp == NULL) {
free(*ptr);
return 0;
}
*ptr = newp;
return 1;
}
later...
char *sp = malloc(20);
void *tmp;
...
tmp = sp;
if (!my_realloc(&tmp, 30))
printf("Failed!\n");
else sp = tmp;
...
and you can do the same even for the types that do not explicitly use
the same representation: int *, or struct pointers, etc. The
assignments to a void * object make it work in a ****table way.
This may not be close enough to what you want to be of any use, but I
hope it clarifies some things.
--
Ben.


|