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 9 of 16 Topic 26091 of 26972
Post > Topic >>

Re: type-punning?

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

j.j.fishbat@[EMAIL PROTECTED]
 wrote:
[...]
> > 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 *bar;
>  :
> foo((void*)bar);
> ---
> 
> is not possible in C?

That's not the same thing.  In your original post, you had:

    static int punme(void **dat,size_t newsize)
    ...
    char *dat = malloc(30);
    int ret = punme((void **)&dat,40);

Here, you are telling punme that is gets passed a pointer to "void *"
but you are passing a pointer to "char *".  Note "pointer to" in both
of those pieces.

[...]
> 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??

But you are not modifying a "generic pointer".  Or, at least, you
are not _passing_ a "generic pointer".

Consider the fact that a "void *" and a "char *" need not be stored
the same way.  Yes, a function can take a "void *" and you can pass
it a "char *", but that is because the value will be converted to a
"void *" before being passed.  However, what you are trying to do is
pass a pointer to "char *", while a pointer to "void *" is expected.
No conversion of your original "char *" will take place.  This is
the same as if you had:

    void foo(double *pt)
    ...
    float f;
    foo((double *)&f);

In your case, the program "works" because, in all likelihood, the
representation of "void *" and "char *" are the same.

-- 
+-------------------------+--------------------+-----------------------+
| 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:33:25 CDT 2008.