QQ <ju****@yahoo.comwrote:
I have one integer array
int A[32];
I need to pass this array into a function and evaluate this array in
this function
how should I pass?
You can't pass an array to a function in C. Variables are passed
always by value, i.e. a copy is made of watever you pass to a
function and that's what the function receives. But this does
not work with arrays - passing an array by value would require
that a copy of the whole array is made, and the inventors of C
decided that this would be too much trouble. But since "passing
an array" to a function is such a useful thing to do, they came
up with the following "replacement": when you try to pass an
array to a function, the address of the first element of the
array is passed to the function instead of the array itself.
Is it fine?
void test(int *a)
{
a[0]=0;
...
}
and call this function as
test(a)
I guess that should be
test( A );
since the array you defined above is called 'A' and not 'a'. And,
yes, basically it's ok since this will pass a pointer to the first
element of 'a' to the function, which has type "pointer to integer",
i.e. what the function expects. But there's a caveat: since the
function receives only a pointer to the first element it has no
knowledge about how long the array is. And without that informa-
tion you might accidentally try to access elements of the array
that are beyond its end. So to make the function safe in this re-
spect you also should pass the length of the array to the function
(of course unless you will never pass an array with less than 32
elements to the function and you thus can use that knowledge when
writing it).
Regards, Jens
--
\ Jens Thoms Toerring ___
jt@toerring.de
\__________________________
http://toerring.de