Thursday, July 30, 2009

Need help understanding a simple C function?

Hi, I know java but am new to C. Although I do have the concept of what pointers are but I am not familiar with the syntax. Would somebody please explain the statements that involve the use of asterisks in the following function?


(Particularly the one that starts with a*)





void allocate_2d_array (int r, int c, double ***a)


{


double *storage;


int i;


storage = (double *) malloc (r * c * sizeof(double));


*a = (double **) malloc (r * sizeof(double *));


for (i = 0; i %26lt; r; i++)


(*a)[i] = %26amp;storage[i * c];


}





Thank you very much for your help.

Need help understanding a simple C function?
Okay, there are two symbols which are relevant in talking about pointers in C (as an aside, in C++ they are slightly extended so people can get confused. In particular %26amp; has one other meaning related to addresses). The two symbols are %26amp; which can be understood as "The Address of" and * which is "Pointer to".





Thus this function has 3 parameters coming in:





r: the number of elements in the array


c: the multiplier


and a pointer to a pointer to a pointer to a. Got that?





The first thing which is declared is a pointer to the array storage. Then we get the counter int, i, but we'll skip over that.





All storage is is an area of memory large enough to hold the address of a double (which is large enough to hold a pointer to an array of doubles, because arrays are usually passed as a pointer to the first element). That's why the storage= line is necessary. It sets aside a block of memory which is r*c*the size of a double bytes long. Since it uses malloc() which returns a void * it is necessary to cast the pointer as a double for it to be useable. That is what the (double *) does.





The next line I can parse better than I can understand. The an area of memory the size of a double * r is allocated, then cast as a pointer to a pointer, which is assigned to the address which is pointed to by a.





The line after that clarifies it a little, if you understand something called pointer decay. What this means is that since parameters are passed as values -- that is new variables are created in each function into which the values in the old variables are copied EXCEPT arrays which are passed as pointers to array[0] -- the address of the first element -- pointers to arrays and arrays can be treated exactly the same way. Each element in the array pointed to by a is assigned the address (the %26amp; operator) of the element in storage at i*c. Thus *a[0]==%26amp;storage[0], *a[1]==%26amp;storage[c], *a[2]==%26amp;storage[2c]... This is why this helps explain the above. It's an array of pointers to doubles.





The subject, as you can tell is very gnarly, and I'm pressed for time, I'm sorry to say. I'll leave you with a link in sources (which I did use) about pointer decay.
Reply:*a is a pointer variable. **a is a pointer to pointer. storage is apointer variable here.


and it has allocated memory dynamically using malloc.


Its syntax to allocate memory is (type of pointer variable)malloc(size).


No comments:

Post a Comment