开发者

pass by value in C

开发者 https://www.devze.com 2023-02-01 07:06 出处:网络
Is the following code portable? I just pass the pointer by value and I can change it in the caller! void foo(void *p)

Is the following code portable?

I just pass the pointer by value and I can change it in the caller!

void foo(void *p)
{
    void **pp = (void**)p;
   开发者_如何学C *pp = "hahaha";
}

int main(int argc,char **argv)
{
    void *p = NULL;
    p = &p;
    printf("%p\n",p);
    foo(p);
    printf("%s\n",(char *)p);     // hahaha
    printf("%p\n",p);

    return 0;
}


Yes, you can change what the pointer points to, but you can only do that because you've made the pointer point to itself:

p = &p;

To answer your question, yes this is portable; but no it's not generally a good idea.


You're always passing a pointer to a pointer by value making it seem a single pointer when you assign it to itself and when you pass it to the function; it works only because you made that pointer point to itself.

What you're doing is basically this:

void foo(void **p)
{
    *p = "hahaha";
}

int main(int argc,char **argv)
{
    void *p = NULL;
    printf("%p\n", &p);
    foo(&p);
    printf("%s\n",(char *)p);     // hahaha
    printf("%p\n", p);
    return 0;
}

with some casts and tricks added. "Card tricks in the dark", I'd say, and definitely not a good idea to put in a real program.

To actually reply to the question: yes, it should be portable, because the standard guarantees that every data pointer can be casted to and from a void * without problems (which is what you do in your code all the time):

A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

(C99, §6.3.2.3.1)


You're actually playing with pointers to pointers. You are still passing the pointer by value and you are not fooling the compiler into changing the value of the caller. What you're changing is just the value pointed to by the pointer to the pointer.

Not a very good idea to use in real life apart from confusing yourself and fellow programmers.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号