Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium



Posted on 2003-12-03
Medium Priority
Last Modified: 2010-04-15
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?
Question by:shorty225
  • 2
LVL 46

Expert Comment

by:Kent Olsen
ID: 9866697

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,

Author Comment

ID: 9866897
i am just asking how to pass arrays through a function and get the average not asking for the complete lab assignment

Accepted Solution

tinchos earned 1500 total points
ID: 9866970
In order to pass an array to a function then you can pass the array (of ints in the example)

float average( int *array, int numberOfElements )
   float average = 0;

    * Iterate through the elements of the array and calculate the average
   return average;

int main()
   int array[] = { 1, 3, 66, 77, 4, 82, 21, 14 };

   float f = average( array, 8 );       /* 8 as there are 8 elements in the array */

  // rest of your code

   return 0;

I guess that this is pretty much how you would pass an array to a function.

Hope this helps

LVL 46

Expert Comment

by:Kent Olsen
ID: 9867162
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,

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question