ph****@yahoo.com (Tweaxor) wrote in message news:<1c**************************@posting.google. com>...
Hey,
I was trying to figure out was it possible in C to pass the values in an array
from one function to another function. Is the possible in C?
It is easy in C. You simply must be aware of a few things first.
ex. y[7] is the array that holds seven values
If possible how could one pass these seven values in the array
to a function that would check the values.
If your function knows y is always going to hold 7 values (and never
less than 7), you can simply pass y as the function's input and not
worry about it.
For example:
#include <stdio.h>
/* print7 takes an array of ints and returns nothing */
void print7(int arr[]);
int main(void)
{
int y[7] = {1,2,3,4,5,6,7};
print7(y);
exit(0);
}
void print7(int arr[])
{
int i;
for(i = 0; i < 7; ++i)
printf("%d\n", arr[i]);
}
If, however, you /don't/ know the size of the array beforehand, you
can do one of two things:
1. Insert a special value at the end of the array so you know you've
reached the end.
2. Pass the size of the array into the function.
In C, strings are implemented the first way: Each string in C is an
array of char that ends with the special value '\0', also called nul.
When a function that works with strings in C reaches nul, it knows the
string has ended.
Functions that work with arrays that are not strings usually take the
length of the array as an additional value.
Let's rewrite our print7 to be printn, which will print arrays of int
of any size we choose:
/* printn takes an array of int and an int and returns nothing. */
void printn(int array[], int size)
{
int i;
for(i = 0; i < size; ++i)
printf("%d\n", array[i]);
}
I tried return y but it didn't work
This opens up another thing you must understand: Temporary storage.
When a function (such as printn) is called, it is given as much
storage as it needs to store all of the local variables it needs (in
this case, the int i). When the function returns, that storage is gone
and can no longer be accessed by anyone unless special steps are
taken.
That special step is the keyword `static': When you make an array
static, you can return it from your function and expect it to be
usable in the function you are returning it to. This is because the
static keyword makes sure the memory is preserved across calls to the
function.
Let's explore this with a trivial function that returns an array of
int:
/* retarr takes nothing and returns a pointer to int (see below). */
int *retarr(void)
{
static int arr[7]; /* arr is static: it will be saved */
int i; /* since i is not static, it will be lost */
for(i = 0; i < 7; ++i)
arr[i] = i;
return arr;
}
Note that retarr actually returns a pointer to int. This is important:
In C, an array is simply an area of memory you can access to store and
retrieve multiple objects of the same type. When you return an array
from a function or pass an array to a function, it `decays' to a
pointer to the first element of that array. Since memory is usually
not preserved once a function exits, the pointer returned when you try
to return an array is a pointer to garbage: It will not point anywhere
useful, and trying to dereference it will lead to an error.
Of course, there is a second way (there's usually more than one way to
do something in C): You can pass in an array (as a pointer) to the
function, and have the function modify that array in place.
A final example:
/* Adds one to all members of an array of ints.
* Takes one array of int and one int. Returns nothing.
*/
void addone(int arr[], int size)
{
int i;
for(i = 0; i < size; ++i)
arr[i] += 1;
}
Remember: When you pass an array to a function, it `decays' to a
pointer to the first element of that array. When you return an array
from a function, it `decays' to a pointer to the first element of that
array.
I hope my post was useful.