Re: type-punning?



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!
.



Relevant Pages

  • Re: back once again...
    ... reference to type ... a, signed char ... int dycObjectP(dyt obj); ... void dycBeginClass; ...
    (comp.lang.misc)
  • passing object reference to the method
    ... and I want to pass it by reference. ... public interface IFoo{} ... void FromHere() ... and compiler screams that it cannot convert IFoo to char (latter ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: type-punning?
    ... newdat) return 1; ... char *dat = malloc; ... The problem is that dat is defined as char *. ... defined as a void *. ...
    (comp.lang.c)
  • Re: pass by reference
    ... char *something; ... You can, if you really need, do something similar to pass by reference, and that is by explicitly passing the address of a variable. ... void AllocateSomething ...
    (comp.lang.c)
  • Re: Passing structs....
    ... void func{ ... int main{ ... char x; ... But a pass by reference in C++ would include the trailing &. ...
    (microsoft.public.vc.language)