Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: type-punnin...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 12 of 16 Topic 26091 of 26972
Post > Topic >>

Re: type-punning?

by Kenneth Brody <kenbrody@[EMAIL PROTECTED] > May 6, 2008 at 04:26 PM

j.j.fishbat@[EMAIL PROTECTED]
 wrote:
> 
> Hi all
> 
> > Consider the fact that a "void *" and a "char *" need not be stored
> > the same way.
> 
> OK, I am the idiot then. I had thought, for the past 10 years
> C programming, that a pointer to char was the same as a
> pointer to int was the same as a pointer to foobar_t, the only
> difference being what it pointed to. And that this fact enables
> one to cast to void* to enable generic programming.
> 
> I see I'll have to rewrite some code.
> 
> Thank you again to all who replied!

Well, you have probably been programming on a platform on which all
of the data pointer types are represented the same way.  (And, I
must confess, so have I.)  However, C does not guarantee such a
thing, and I believe others have posted here examples of systems
in which they are, in fact, represented differently.

Yes, you can convert from any pointer to "void *" and back again.
But, you can't, for example, convert from "int *" to "void *" and
then to "long *", and dereference that pointer without invoking
UB.  (In fact, the converting to "long *" step itself may be UB.)

Note, too, that Mr. van D?k (sorry about the mangling of the name,
but my newsreader is obviously not UTF-8 compliant) says that the
standard does say that "void *" and "char *" are special cases
which must be represented the same way.  If that is true, then
your original example will "work" as-is, but only because you
are converting "char **" to "void **".  It's not guaranteed to
work with, for example, "int **" to "void **", in a "****table"
manner.  Again, it will probably "work" on your systems, because
they are likely to be storing "void *" and "int *" in the same
representation.

-- 
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody        | www.hvcomputer.com | #include              |
| kenbrody/at\spamcop.net | www.fptech.com     |    <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@[EMAIL PROTECTED]
>
 




 16 Posts in Topic:
type-punning?
j.j.fishbat@[EMAIL PROTEC  2008-05-06 08:46:00 
Re: type-punning?
vippstar@[EMAIL PROTECTED  2008-05-06 09:43:53 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 19:35:00 
Re: type-punning?
Andrey Tarasevich <and  2008-05-06 11:05:25 
Re: type-punning?
lawrence.jones@[EMAIL PRO  2008-05-06 14:39:47 
Re: type-punning?
j.j.fishbat@[EMAIL PROTEC  2008-05-06 11:17:38 
Re: type-punning?
Andrey Tarasevich <and  2008-05-06 11:24:55 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 20:32:01 
Re: type-punning?
Kenneth Brody <kenbrod  2008-05-06 14:34:57 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 20:50:08 
Re: type-punning?
j.j.fishbat@[EMAIL PROTEC  2008-05-06 11:56:36 
Re: type-punning?
Kenneth Brody <kenbrod  2008-05-06 16:26:19 
Re: type-punning?
Kaz Kylheku <kkylheku@  2008-05-06 14:02:24 
Re: type-punning?
Harald van =?UTF-8?b?RMSz  2008-05-06 23:11:51 
Re: type-punning?
"Default User"   2008-05-06 22:49:07 
Re: type-punning?
Ben Bacarisse <ben.use  2008-05-06 22:17:42 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:24:50 CDT 2008.