1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

HELP: Function with array passing

Discussion in 'C' started by neelaka, May 13, 2008.

  1. neelaka

    neelaka New Member

    Joined:
    May 13, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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]);;
    }
     
    Last edited by a moderator: May 14, 2008
  2. jayaraj_ev

    jayaraj_ev New Member

    Joined:
    Aug 29, 2007
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software engineer
    Location:
    Bangalore
    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.
     
  3. neelaka

    neelaka New Member

    Joined:
    May 13, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     

Share This Page