Pass array to function

W

wilson

Dear all,

In this time, I want to pass array to function.
What should I declare the parameter in the function?i
int array[ ] or int array[4]? Which one is correct?

/********************************************************
Below is my code:
********************************************************/

void pass(int var, int element, int array[ ])
{
var = 1;
element = 1;
array[0] = 1;
}

int main()
{
int variable = 0;
int array_element[2] = {0, 0};
int whole_array[2] = {0, 0};

pass(variable, array_element[0], whole_array);

printf("variable=%i ; array_element[0]=%i ; whole_array[0]=%i\n",
variable, array_element[0], whole_array[0]);

system("pause");
return 0;
}
 
E

Emmanuel Delahaye

wilson wrote on 13/08/04 :
Dear all,

In this time, I want to pass array to function.
What should I declare the parameter in the function?i
int array[ ] or int array[4]? Which one is correct?

Both are correct. int *array is correct too and allows more control
like

int * array
int const * array
int * const array
int const * const array

It's also a good idea to pass the number of possible elements of the
array to the function for controls.

size_t n
/********************************************************
Below is my code:
********************************************************/

void pass(int var, int element, int array[ ])
{
var = 1;
element = 1;

It is generally pointless or the evidence of a design error to modify
the value of a parameter. If it is a parameter, the value is supposed
to come from the caller. If the value belongs to the function, you
don't need a parameter, but probably a constant or a local variable.

Note: keep in mind that in C, the parameters are always passed by-value
(a local copy of the original value). Obviously, changing the copy
doesn't change the original.
array[0] = 1;
}

int main()
{
int variable = 0;
int array_element[2] = {0, 0};
int whole_array[2] = {0, 0};

pass(variable, array_element[0], whole_array);

printf("variable=%i ; array_element[0]=%i ; whole_array[0]=%i\n",
variable, array_element[0], whole_array[0]);

system("pause");
return 0;
}
 
J

Joona I Palaste

wilson said:
Dear all,
In this time, I want to pass array to function.
What should I declare the parameter in the function?i
int array[ ] or int array[4]? Which one is correct?

Either. But the function will not actually receive an array - it will
receive a pointer to its first element. You can access this like an
array, but you'll have to know its size yourself, the parameter won't
tell you.
/********************************************************
Below is my code:
********************************************************/
void pass(int var, int element, int array[ ])
{
var = 1;
element = 1;

These two lines will have no effect outside the function.
array[0] = 1;
}
int main()
{
int variable = 0;
int array_element[2] = {0, 0};
int whole_array[2] = {0, 0};
pass(variable, array_element[0], whole_array);
printf("variable=%i ; array_element[0]=%i ; whole_array[0]=%i\n",
variable, array_element[0], whole_array[0]);
 
T

Toby Newman

# Joona I Palaste
wilson <[email protected]> scribbled the following:
void pass(int var, int element, int array[ ])
{
var = 1;
element = 1;

These two lines will have no effect outside the function.
array[0] = 1;
}

And the third will? I understand that:

var = 1; // and
element = 1;

are only modifying local copies of the original variables fed to the
functon. Your lack of comment on the third line
array[0] = 1;
implies to me that it is the original array[0], not a copy, that is set
to '1' here. Is that so?
 
J

Joona I Palaste

Toby Newman said:
# Joona I Palaste
wilson said:
void pass(int var, int element, int array[ ])
{
var = 1;
element = 1;

These two lines will have no effect outside the function.
array[0] = 1;
}

And the third will? I understand that:
var = 1; // and
element = 1;
are only modifying local copies of the original variables fed to the
functon. Your lack of comment on the third line
array[0] = 1;
implies to me that it is the original array[0], not a copy, that is set
to '1' here. Is that so?

Yes, the third line will have very much effect outside the function. The
parameter "array" is a copy of the original argument, yes, and altering
the parameter array won't have any effect outside the function. But! The
copy and the original argument initially have the same value, and as
their type in a value context is a pointer, this means they point to the
same place. Thus, altering array[0] (which is the same thing as altering
*array) will alter the memory location these pointer values point to,
which is the same memory location for both the copy and the original
argument.
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top