How to Use Arrays and Functions Together in C Programming

By Dan Gookin

In C programming, creating an array for use inside a function works just like creating an array for use inside the main() function: The array is declared, it’s initialized, and its elements are used. You can also pass arrays to and from functions, where the array’s elements can be accessed or manipulated.

How to pass an array to a function

Sending an array off to a function is pretty straightforward. The function must be prototyped with the array specified as one of the arguments. It looks like this:

void whatever(int nums[]);

This statement prototypes the whatever() function. That function accepts the integer array nums as its argument. The entire array — every element — is passed to the function, where it’s available for fun and frolic.

When you call a function with an array as an argument, you must omit the square brackets:

whatever(values);

In the preceding line, the whatever() function is called using the array values as an argument. If you keep the square brackets, the compiler assumes that you meant only to pass a single element and that you forgot to specify which one. So this is good:

whatever(values[6]);

But this is not good:

whatever(values[]);

The code shown in Mr. Function, Meet Mr. Array features the showarray() function that eats an array as an argument. It’s a void function, so it doesn’t return any values, but it can manipulate the array.

MR. FUNCTION, MEET MR. ARRAY

include <stdio.h>
#define SIZE 5
void showarray(int array[]);
int main()
{
 int n[] = { 1, 2, 3, 5, 7 };
 puts("Here's your array:");
 showarray(n);
 return(0);
}
void showarray(int array[])
{
 int x;
 for(x=0;x<SIZE;x++)
 printf("%dt",array[x]);
 putchar('n');
}

The showarray() function is called at Line 12. See how the n array is passed without its angle brackets? Remember that format!

At Line 16, the showarray() function is declared with the array specified using square brackets, just like the prototype at Line 5. Within the function, the array is accessed just like it would be in the main() function, which you can see at Line 21.

Exercise 1: Type the source code from Mr. Function, Meet Mr. Array into your editor. Build and run the program to ensure that it works.

Exercise 2: Add a second function, arrayinc(), to your source code from Exercise 1. Make it a void function. The function takes an array as its argument. The function adds 1 to each value in the array. Have the main() function call arrayinc() with array n as its argument. Then call the showarray() function a second time to display the modified values in the array.

How to return an array from a function

In addition to being passed an array, a function in C can return an array. The problem is that arrays can be returned only as pointers. But that’s not the worst part:

The scandalous truth is that C has no arrays — that they are merely cleverly disguised pointers. Array notation does have its place, but pointers are where the action is.