Yes we can. Just check the bedding. I think you have an idea.
The problem with using blahhow you write is that when you return to the main one, not only the contents of the buffer (correctly, how you do it) change, but also the pointer buf. It may not be what you want, and definitely not what you need to free.
Test:
#include <stdlib.h>
void dref1(char **blah)
{
(*blah)[0] = 'a';
(*blah)[1] = 'z';
(*blah)[2] = 0x00;
}
void dref2(char **blah)
{
char *p = *blah;
*p++ = 'w';
*p++ = 't';
*p = 0x00;
}
void dref3(char **blah)
{
*(*blah)++ = 'w';
*(*blah)++ = 't';
*(*blah) = 0x00;
}
void dref4(char **blah)
{
**blah = 'q';
(*blah)++;
*((*blah)++) = 'w';
}
int main(void)
{
char *buf = (char*)malloc(3);
char *buf1=buf;
dref1(&buf);
puts(buf);
dref2(&buf);
puts(buf);
dref3(&buf);
puts(buf);
puts(buf1);
buf=buf1;
dref4(&buf);
puts(buf);
puts(buf1);
free(buf1);
return 0;
}
source
share