John Gordon
8/4/2011 10:37:00 PM
In <9396329e-3bbf-4e47-af8c-d44f295c75ee@a10g2000yqn.googlegroups.com> mt <mahdert@gmail.com> writes:
> I am trying to understand the following code and I am having some
> problems..
> 1. What is the meaning of double*** pdata? Why the need to declare a
> pointer to a pointer to a pointer?
> 2. same thing for int*** pmask
> Please elucidate this for me.
In general, if you want to pass an object into a function and have the
function change that object's value, you must pass a pointer-to-the-object
instead of passing the object directly.
For example, this won't work:
void set_value_to_seven(int i)
{
i = 7;
}
int main(void)
{
int x = 5;
set_value_to_seven(x);
printf("x is now %d\n"); // will print "x is now 5"
}
However, this will work:
void set_value_to_seven(int *i)
{
*i = 7;
}
int main(void)
{
int x = 5;
set_value_to_seven(&x);
printf("x is now %d\n"); // will print "x is now 7"
}
Along similar lines, if you want to pass a pointer into a function which
malloc's space for the pointer, it won't work if you pass it directly:
void allocate_ten_ints(int *i)
{
i = malloc(10 * sizeof(int));
}
int main(void)
{
int *x = NULL;
allocate_ten_ints(x);
x[0] = 5; // will crash because i is still NULL
}
But it will work if you pass a pointer-to-the-pointer:
void allocate_ten_ints(int **i)
{
*i = malloc(10 * sizeof(int));
}
int main(void)
{
int *x = NULL;
allocate_ten_ints(&x);
x[0] = 5; // will work
}
Look closely at these lines in makedatamask:
if (i==nrows) /* break not encountered */
{ *pdata = data;
*pmask = mask;
return 1;
}
*pdata = NULL;
*pmask = NULL;
This function wants to modify the value of pdata -- either by setting it
to point to data, or by setting it point to NULL. Therefore the caller
must pass a pointer-to-pdata. I'm assuming pdata is declared like this
in makedatamask()'s caller function:
double **pdata;
Therefore, since the caller wants makedatamask to change pdata's value,
it must pass a pointer-to-pdata, so makedatamask gets an argument of
type "double ***".
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"