Alexi Alexi - 1 year ago 109
C Question

Passing arrays to functions! Confusions! in C programming

Example array:

int hourlyTemperatures[ 24 ];

So in function call we write the array name and its size!

modifyArray( hourlyTemperatures, 24 )

we know the above mentioned is the right way to pass array to function!

but in this code below I see we are calling mean function and passing array without giving its element size like
mean (response);

So my question is which one is correct? Passing array with its element size or just passing array with it’s name? And why the two method is used?

#define SIZE 99

void mean(const unsigned int answer[]);

int main(void)
unsigned int response [SIZE] =

mean (response);

void mean (const unsigned int answer[])
size_t j;
unsigned int total = 0;

printf("%s\n%s\n%s\n", "********", "Mean", "********");

// total response values
for(j=0; j<SIZE; ++j){
total += answer[j];

printf ("The mean is the average value of the data items!\n"
"The mean is the equal to the total of all data items\n"
"divided by the number of data items (%u)\n\n", SIZE);

printf("The mean value for this run is: %u / %u = %.4f\n\n", total, SIZE, (double)total/SIZE );


Answer Source

The function call modifyArray(hourlyTemperatures, 24) passes a pointer to the first int stored in the array hourlyTemperatures[], so the function knows where to find the first value, and the int 24, so the function knows how big the array is to avoid walking off the end of the array.

The function call mean(response) passes a pointer to the first unsigned int stored in the array response[], but the function needs another way to know how large the array is, since no further information is passed by the caller.

The preprocessor directive #define SIZE 99 is used when you compile the program, and any occurrence of the characters 'SIZE' is replaced with the characters '99' in the source before the actual compilation, so the function is interpreted with the line:

for (j = 0; j < 99; ++j) {...

In this way the mean() function knows how large the array is. Instead of using the #define, you could also put the line:

int size = 99;

before the function definitions. This would make size a global variable visible in any function below it, so you could write:

for (j = 0; j < size; ++j) {...

But, since global variables are generally a bad thing, you should usually pick the first method, explicitly passing the size of the array to the function that processes the array.

So, to recap:

1) It is usually best to pass the size of the array explicitly to the processing function: modifyArray(hourlyTemperatures, 24). This way you always know what size the array is, and you can work with arrays of different sizes: 'modifyArray(hourlyTemperatures, 25)`, for example, if you have added a temperature measurement.

2) You can define a constant to store the size of the array, which is then used in the array processing function: #define SIZE 99. One downside of this is that if you want to work with an array with 100 elements, you have to change the source code so that #define SIZE 100, and then recompile.

3) You can declare a global variable to store the size of the array, and use the global variable in the array processing function. The global variable is accessible to any function below the declaration in the source file. This method is error-prone: another function might inadvertently change the value of size, giving you a nasty surprise when you call mean.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download