Link to home
Start Free TrialLog in
Avatar of shorty225
shorty225

asked on

arrays

i have a topic where i have to pass an array of numbers through a function using the information coming from a disk
and then take the average of the numbers

how would i do this?
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


Hi shorty225,

This is obviously a homework assignment and the membership agreement prohibits us from doing your homework for you.  If you've got a specific problem or question we can certainly help you with that.  We can also help you correct code that is giving you trouble.  If you make an attempt to code something you'll get no shortage of answers from people willing to help.

Your question is a bit too brief to get much in the way of "general help".  If you'll post more of the assignment we'll give you some guidelines.

Good Luck,
Kent
Avatar of shorty225
shorty225

ASKER

i am just asking how to pass arrays through a function and get the average not asking for the complete lab assignment
ASKER CERTIFIED SOLUTION
Avatar of tinchos
tinchos

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi shorty,

We've also got a small "language barrier" here.  Passing an array "through a function" is a mathematical term that describes a process, such as passing data through an fft (Fast Fourier Transform).  In C, data is passed TO a function.  The function can then use the data and/or pass it to another function.  If the function passes the data to another function, then technically, the data will have been passed "through" the first function, but I doubt that is what you meant.

Parameter passing in C comes in two flavors.  You can pass a value or you can pass an address.  (Passing an address is often called passing by reference.  Some purists claim that C doesn't pass by reference, others claim that it does.  The argument is certainly beyond the scope of your question.)

When you pass a "native" variable like a char, short, int, long, float, or double you are passing a value.  The value of the variable is placed on the stack and the called function uses the value as if it belonged to it.  Actually, it does.  The variable is created on the stack specifically FOR the called function.

When you pass arrays, you pass by address.  The compiler does NOT copy the entire array to stack.  It copies only the address of the array.  This is certainly much faster than copying a potentially large amount of data, but has the drawback that the called function can actually read and write the original data, not a copy as what happens when you pass by value.

To pass an address (such as an array), the function parameter(s) simply define the parameter as an address.  You do that with the asterisk (*) or array notation ([]).  Note that you can "mix and match" parameters -- some parameters can be "by value" and other can be "by address".  But any one parameter needs to be consistent throughout your program.

myFunction(char *string)
{
}

This function declaration says that the address of a string is going to be passed.  You can call this function several different ways.

myFunction ("This is a sample string.");

char myArray[30] = {"This is also a sample string.");

myFunction (myArray);


For many usages, the asterisk and array notation are interchangable.

myFunction (char *string)

is equivalent to

myFunction (char string[])

The compiler know that the parameter being passed is the address of the first character of a string.  Within a function that is declared with either style, you can also use either the pointer notation or the array notation to access elements of the string.

myFunction (char *string)
{
  if (*string == string[1])  /*  test if the first two characters are the same  */
...
}

This is perfectly legal.  Though mixing pointer and index notation can be confusing.

Of course, you can pass arrays of other data types, too:

tableFunction (int Array[]);
matrixFunction (int Array[10][25]);

Note that if the array has only one index (a one dimensional array) you do not need to declare the size of the array.  This is because the compiler knows how to reference any element of the array.  C doesn't do "bounds checking" so there is no need to tell the function how large the array is.

But if the array has multiple indices you must tell the function the size of the array.  The concept of a two-dimensional table means nothing to C because memory is consecutive.  C needs to be able to convert your multi-dimensional structure to an offset into a block of memory and the only way that this can happen is if the function knows the size of your table.

So to wrap up, you pass an array by telling the function the starting address of the data.

Good Luck,
Kent