Re: type-punning?
- From: j.j.fishbat@xxxxxxxxx
- Date: Tue, 6 May 2008 11:17:38 -0700 (PDT)
Hi
This doesn't fix the problem. This merely reorganises the code in a form
that the compiler may happen to not warn about.
The problem is that dat is defined as char *. You can't pretend it's
defined as a void *. The language doesn't let you.
are you saying that
---
int foo(void* bar)
{
char *st = (char*)bar;
:
}
:
:
char *bar;
:
foo((void*)bar);
---
is not possible in C?
Are we using the same language?
A fixed version looks
like
#include <stdlib.h>
#include <stdio.h>
static void *punme(void *dat, size_t newsize) {
return realloc(dat, newsize);
}
int main (void)
{
char *dat = malloc(30);
char *newdat = punme(dat, 40); /* or call realloc directly */
if (newdat != NULL) dat = newdat;
printf("punme would have returned %d\n", (newdat == NULL ? 1 : 0));
return 0;
}
Part of the "inessential detail" omitted is that
I use the return value of punme() for other purposes,
I specifically want to modify dat (char*) which I pass
to punme() by reference.
Is there really no way at all to modify a generic
pointer by passing a reference to it to a function?
Are C programmers condemned to return structs
(one member of which is the void* modified) whenever
we want to do generic programming??
Thanks!
.
- Follow-Ups:
- Re: type-punning?
- From: Ben Bacarisse
- Re: type-punning?
- From: Kaz Kylheku
- Re: type-punning?
- From: Kenneth Brody
- Re: type-punning?
- From: Harald van Dijk
- Re: type-punning?
- From: Andrey Tarasevich
- Re: type-punning?
- References:
- type-punning?
- From: j . j . fishbat
- Re: type-punning?
- From: vippstar
- Re: type-punning?
- From: Harald van Dijk
- type-punning?
- Prev by Date: Re: lcc first experience
- Next by Date: Re: [OT] lcc first experience
- Previous by thread: Re: type-punning?
- Next by thread: Re: type-punning?
- Index(es):
Relevant Pages
|