strong/weak typing and pointers

Steven Bethard steven.bethard at gmail.com
Thu Nov 4 12:32:21 EST 2004


Diez B. Roggisch <deetsNOSPAM <at> web.de> writes:
> 
> I think what Alex means is that if you allow for re-interpreting data as
> simply byte arrays and the other way round, you end up with exactly the
> weak typing you defined before: The arbitrary reinterpretation of memory
> portions.
> 
> Consider this simple example:
> 
> int main() {
>   float f = 13345.0;
>   void *mem = (void*)&f;
>   int *i = ((int*)mem);
>   printf("%f, %i, %d\n", f, mem, *i);
> }

Ahh, ok, I understand where he was going now, thanks.

However, this doesn't really address my concern.  Clearly, allowing you to treat
things as untyped *allows* you to cast one type of structure to another, but it
definitely doesn't *require* you to do so.  In your example, yes, casting to
(void*) lets you cast a piece of memory back and forth between float and int,
but I still don't know when I would actually want to do that...

Does this make my concern any clearer?  What I'm asking for is an example like
yours above that not only shows that you can treat, say, the bits of a float as
an integer, but also shows why this would be useful.

Thanks again,

Steve




More information about the Python-list mailing list