473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pass array to function

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
5 3398
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
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
# 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
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
Thanks, Emmanuel.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
6
by: Kenny | last post by:
Hello, can anyone tell me how to pass an array to a function ? I have this function , part of my class. It works if I do not put in int a everywhere , but obviously , I need to add an array so I...
2
by: Te-Jung Lo | last post by:
the following is the original code i wrote #include <stdio.h> #include <stdlib.h> //--- Prototype ---// void bfs(int ); void main()
1
by: Mark Dicken | last post by:
Hi All I have found the following Microsoft Technet 'Q' Article :- Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure (I've also copied and pasted the whole contents into...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
10
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
3
by: QQ | last post by:
I have one integer array int A; 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)
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.