![]() |
HELP: Function with array passing
Please help find the error in this code.
It probably obvious but I cant seem to find the mistake. Its a simple call to a function with an array address, but somehow the subsequent addresses of the array elements get messed up. Please help, urgently needed for a school project. Thanks a lot. ________________________________________ Code:
#include <math.h> |
Re: HELP: Function with array passing
Hi,
In your code getNormal () U r passing address of an array vector3D[3] .So to fill the values in each row of vector3D[3] you should fill it like this (*normal)[0]=-10; (*normal)[1]=-2; (*normal)[2]=2; because u are not a passing a two dimensional array right! Inside the function when normal is of type float[*][3] which means normal can take more rows of float[3]. when u find the address of normal normal[0] = first address float[0][3] normal[1] = first addr + size of (vector3D) normal[2] = first addr + size of (vector3D) + size of (vector3D) so u r trying to find the addres of norma[0][3],normal[1][3], normal[2][3]. for eg The addresses inside the function: 2293552 2293564 2293576 The values inside the function: -10.000000 0.000000 4.000000 when you note down the address values they will have a difference of 12. out side the function u r accessing a single dimensional array vector3D[3] and trying to find out the adress of norma[0],normal[1], normal[2]. The addresses returned : 2293552 2293556 2293560 The values returned : -10.000000 -2.000000 2.000000 from this eg you can also see that address of normal[2] = 2293560 & normal[1][3] = 2293564. |
Re: HELP: Function with array passing
Thanks a lot for the reply,
I got it to work by specifying like this. I was making a fundamental mistake of thinking something else when infact the name of the array is a pointer itself. void getNormal(vector3D normal, point3D p1, point3D p2, point3D p3) {} void main() { vector3D n; getNormal(n, p0, p1, p2); } Thanks again. Cheers |
| All times are GMT +5.5. The time now is 11:29. |