how to pass this parameter into the function

Q

QQ

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?
Is it fine?

void test(int *a)
{
a[0]=0;
....
}
and call this function as

test(a)

thanks a lot
 
W

Walter Roberson

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?
Is it fine?
void test(int *a)
{
a[0]=0;
...
}
and call this function as

Close, but outside of test(), the variable 'a' declared in test()
does not exist, and you haven't done anything with your variable 'A'
that does exist.

You probably want to call

test(A);
 
M

Malcolm McLean

QQ said:
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?
Is it fine?

void test(int *a)
{
a[0]=0;
...
}
and call this function as

test(a)

thanks a lot

#include <stdio.h>
void foo(int *ptr, int N);

int main(void)
{
int i;
int array[10] = {1,2,3,4,5,6,7,8,9,10};
foo(array, 10);
for(i=0;i<10;i++)
printf("%d\n", array);
}

void foo(int *ptr, int N)
{
int i;

for(i=0;i<N;i++)
ptr += 2;
}
 
J

Jens Thoms Toerring

QQ said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top