# HELP: Function with array passing

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

1. ### neelakaNew Member

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

Thanks a lot.

________________________________________

Code:
```#include <math.h>
#include <stdlib.h>
#include <stdio.h>

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

void getNormal(vector3D *normal, point3D p1, point3D p2, point3D p3)
{
*normal=-10;
*normal=-2;
*normal=2;

printf("The addresses inside the function: %i  %i  %i \n", normal, normal, normal);

printf("The values inside the function:  %f  %f   %f \n\n", *normal, *normal, *normal);
}

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, &n, &n);

printf("The values returned :  %f  %f   %f \n\n", n, n, n);;
}```

Last edited by a moderator: May 14, 2008
2. ### jayaraj_evNew Member

Joined:
Aug 29, 2007
Messages:
20
2
Trophy Points:
0
Occupation:
Software engineer
Location:
Bangalore
Hi,
In your code getNormal () U r passing address of an array vector3D .So to fill the values in each row of vector3D you should fill it like this
(*normal)=-10;
(*normal)=-2;
(*normal)=2;

because u are not a passing a two dimensional array right!

Inside the function when normal is of type float [*] which means normal can take more rows of float.
when u find the address of normal
normal = first addr + size of (vector3D)
normal = first addr + size of (vector3D) + size of (vector3D)
so u r trying to find the addres of norma,normal, normal.

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

and trying to find out the adress of norma,normal, normal.

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 = 2293560 & normal = 2293564.

3. ### neelakaNew Member

Joined:
May 13, 2008
Messages:
2
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