423,851 Members | 1,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,851 IT Pros & Developers. It's quick & easy.

Pass array to function

P: n/a
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;
}
Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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;
}


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2

P: n/a
wilson <wi*******@hotmail.com> scribbled the following:
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]); system("pause");
return 0;
}


--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"How can we possibly use sex to get what we want? Sex IS what we want."
- Dr. Frasier Crane
Nov 14 '05 #3

P: n/a
# Joona I Palaste
wilson <wi*******@hotmail.com> 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?

--
Toby
Nov 14 '05 #4

P: n/a
Toby Newman <go****@asktoby.com> scribbled the following:
# Joona I Palaste
wilson <wi*******@hotmail.com> 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?


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"How can we possibly use sex to get what we want? Sex IS what we want."
- Dr. Frasier Crane
Nov 14 '05 #5

P: n/a
Thanks, Emmanuel.
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.