Hello, this is my first post. I'm fairly new to programming and i have some uncertainty regarding the manipulation of arrays through a function that they are passed to.
The context is a matrix transformation, where i want to call a function with an array m[4][4] and a few transformation parameters as its arguments.
Code:
void matrixTransform(double *m, double rx, double ry, double rz, double tx, double ty, double tz)
{
/* Here i do a few mathematical calculations.
Essentially, i want to set m = m * mt, where mt is an array[4][4]
that is built from the (rx, ry, ...) parameters on each function
call.
*/
}
I have browsed related questions here on the forum and elsewhere on the web, but they don't address my confusion, so i though i'd go through it step by step to finally get clarity.
My question is regarding the proper syntax for handling arrays passed to functions. (1) Do i understand it correctly, that an asterisk needs to be placed before first parameter m, because the function is to expect an array (pointer)?
(2) And are these two lines equivalent in meaning/behavior?
Code:
void matrixTransform(double *m) {...}
void matrixTransform(double m[]) {...}
(3) I have also seen the use of
Code:
function(double **var)
in relation to array handling, which confused me even more.
(4) Since my code does an (abstractly described) m = m * mt assignment, i suspect i need to create a copy of m within the function, so that i can read from it's original contents, while writing new ones to it.
e.g.
Code:
void matrixTransform(double *m)
{
/* (again, pseudo-code because it's redundant to type it all out)
these are 2D arrays of size [4][4]. */
m2 = m;
m = m2 * mt;
}
(5) What would be a robust but fast method to create this copy? A simple declaration of m2 followed by a for-loop to transfer m's contents to it?
Regarding array manipulation, if i write this code:
Code:
void matrixTransform(double *m)
{
m[2][3] = 5;
}
(6) Have i effectively changed a value in m's original address space? I imagine this would be most desirable for an array that iteratively gets its values modified, but where size (16 x 8 bytes) stays constant.
(7) Also, am i right in thinking the function can be of type void, as i'm not returning a value but manipulating a pre-existing array?
Sorry for all the questions. Not 'getting it', i simply don't want to use trial and error in C; it's too easy to mess up.
Adrian