Re: doubt about reference
On May 30, 4:25 pm, "cuihongmeng" <cuihongm...@gmail.com> wrote:
hi all!
please explain the deference between the two functions .
thx.
******************************************************
void fun(char *s)
{
s=(char *)malloc(10*sizeof(char));
strcpy(s,"XXXXXXXX");
}
void fun(char *&s)
{
s=(char *)malloc(10*sizeof(char));
strcpy(s,"XXXXXXXX");
}
Hi
I can explain the "difference" between pointer and reference to
pointer
with the following piece of code:
int i = 1;
int& ir = i; // reference to int
int* ip = &i; // pointer to int
int*& ip2 = ip; // reference to pointer to int
// ip2 is another name for ip
ir++; // i = 2
(*ip)++; // i = 3
(*ip2)++; // i = 4
like int and reference to int, we can have pointer to int and
reference to pointer to int.
About your code:
1. To avoid memory leak, use free(s) when you don't need
to s anymore.
2. You can't use both functions in your code:
char* s = NULL;
fun(s); // ambiguous call to overloaded function.
Regards,
-- Saeed Amrollahi