Passing & Using Array Addresses in C Programming

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In this lesson, you will learn how to pass arrays to functions in C. We will be passing functions by address, and exploring the implications of this approach. Working code examples are provided.

Passing Array Addresses in C

Consider that you are writing an application for a payroll system. If you pass an employee's pay rate to a function to calculate weekly pay, would you want that function to be able to change the pay rate? 99% of the time, the answer is no.

If you were working on a baseball video game and needed a function to re-order the roster, you would probably be fine with that function changing the actual roster.

These examples highlight pass by value and pass by reference processes, respectively. In the first case, a copy of the pay rate variable is passed to the function. In the second example, we pass a copy of the address (a pointer) to the function. Now the function can update the underlying structure or variable.

Since a batting roster is a good example of an array, let's look at how array addresses can be passed by value in C.

Arrays

Before we get into the nuts and bolts, let's first create the shell for the C program. This will set up the main function and declare our roster of batters. The integer values represent jersey numbers.

#include <stdio.h>
int main() {
  int batters[] = {2, 6, 8, 34, 42};

Now we have an array, but we still need to create a function to swap out the roster. Depending on your programming style, you can create functions in one of two ways:

  1. Create all functions before the main function
  2. Declare all functions before main, and then define them after main

Since the main function is the engine of the program, many programmers go with option 2. This way you have a nice list of the functions that are included in the program. Let's go with that option.

Right after the include statement, add the following function declaration:

void switch_batters(int *b);

The asterisk in front of the variable means that b is a pointer. This means that b's value actually points to the address of another variable. When we use it, it will point to the batters array, and we can then change that underlying array.

Next, let's create the function, placing it after the last curly bracket of main. The parameter here (b) matches what we declared (a pointer) which points to the underlying array.

//function to switch
void switch_batters(int b[5]) {
  //swap batters around
  int batters2 = {42, 8, 34, 6, 2};
  int i;
  for(i = 0; i < 5; i++) {
    b[i] = batters2[i];
  }
}

Now let's see how this all works. Just after the array declaration for the batters in main, add the following lines of code. The first for loop shows the array as-is. Next, we call the switch_batters function. Note how we only need to pass in the name of the array. This is because the pointer will reference back to it. Finally, we run another for loop to show what the roster looks like now.

 printf("Original batters: \n\n");
  int i;
 for(i = 0; i < 5; i++) {
  printf("%d ", batters[i]);
 }
 //call function
 switch_batters(batters);
 printf("\nNew Roster : \n\n");
 for(i = 0; i < 5; i++) {
  printf("%d ", batters[i]);
 }

When you compile and run this program, the output should be:

Original batters
2 6 8 34 42
New Roster:
42 8 34 6 2

We have successfully updated the original array. This highlights the power of passing by reference or address.

Adding another Parameter

So far we have a pretty static array of 5 batters. The switch_batters function is locked in at 5. In reality, a roster may have up to 25 players. Our function is limited. We can add another parameter to the function that stores the length of the array being passed to it.

When compiled, the following program will ask to enter the jersey numbers of up to 25 players, then displays those numbers on screen.

To unlock this lesson you must be a Study.com Member.
Create your account

Register for a free trial

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Free 5-day trial

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it free for 5 days!
Create an account
Support