This is the prototype for the function, sum, I am using to summate the elements of two arbitrary arrays.
double **sum(double **A, double **B, int M, int N);
They’re both passed as double-pointers (don’t ask why) and M and N are supposed to be the dimensions of the arrays respectively.
What I’m having trouble doing is, within the implementation of the function, accessing elements of the arrays. I’ve tried “deferencing” the array (or so I think) but to no avail. Below are the things I’ve tried:
> *A; // Gives me an address, 0xf5c28f5c, not a number
> **A; // Error
> &A; // Gives me the address of A
This is how I’m passing the arrays to sum if it’s helpful:
double a[] = {5.34, 5.23, 6.5, 3.51};
double b[] = {74.3, 42.1, 642.2, 54.2};
sum( (double **) a, (double **) b, 4, 4);
So the question is, how exactly am I supposed to get the elements in the arrays when passed by pointer/double-pointer?
Your array passage to your function is entirely incorrect. A double pointer is best to be thought of as a pointer-to-pointer. In the same way a pointer is a variable that holds the address to memory someplace, a pointer-to-pointer holds a similar address, it just so happens the memory it references is the memory occupied by another pointer. This is best described by example:
On my MBA 64bit, this give us:
Get a grasp on that and I think you’ll find your issues in your code, the first of which is the hard cast to a double pointer of ‘a’ and ‘b’. Maybe try their addresses instead. Secondly, you will likely want to revisit how you’re using those pointers for dereferences in your function, because chances are that needs work too.