Passing pointer argument by reference under C?

前端 未结 6 1972
清歌不尽
清歌不尽 2020-12-01 22:20
#include 
#include 

void
getstr(char *&retstr)
{
 char *tmp = (char *)malloc(25);
 strcpy(tmp, \"hello,world\");
 retstr = tmp;
}         


        
6条回答
  •  悲&欢浪女
    2020-12-01 22:38

    There is an interesting trick in libgmp which emulates references: typedef mpz_t __mpz_struct[1];

    and then you can write like this:

    mpz_t n;
    mpz_init(n);
    ...
    mpz_clear(n);
    

    I would not recommend to use this method, because it may be incomprehensible for others, it still does not protect from being a NULL: mpz_init((void *)NULL), and it is as much verbose as its pointer-to-pointer counterpart.

提交回复
热议问题