Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   HELP: Function with array passing (http://www.go4expert.com/forums/help-function-array-passing-t10591/)

neelaka 14May2008 01:36

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>
#include <stdlib.h>
#include <stdio.h>

//Declarations
typedef float point3D[3]; // declare a point data type for 3D points
typedef float vector3D[3]; // declare a vector data type for 3D vectors


void getNormal(vector3D *normal, point3D p1, point3D p2, point3D p3)
{
        *normal[0]=-10;
        *normal[1]=-2;
        *normal[2]=2;
       
        printf("The addresses inside the function: %i  %i  %i \n", normal[0], normal[1], normal[2]);
       
        printf("The values inside the function:  %f  %f  %f \n\n", *normal[0], *normal[1], *normal[2]);       
}


void main()

{
                //Let P0 = (1, 0, 2)        P1 = (2, 3, 0)        P2 = (1, 2, 4)
        point3D
                p0 = {1,0,2},       
                p1 = {2,3,0},
                p2 = {1,2,4};

        vector3D n;

        getNormal(&n, p0, p1, p2);


        printf("The addresses returned : %i  %i  %i \n", &n[0], &n[1], &n[2]);
       
        printf("The values returned :  %f  %f  %f \n\n", n[0], n[1], n[2]);;
}


jayaraj_ev 17May2008 18:49

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.

neelaka 19May2008 10:41

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 04:35.