Passing Arrays


Let's try another, more practical example. What if you wanted to pass an array of integers to a function? The solution, of course, lies in passing pointers. As you may or may not know, the name of an array is a pointer to the first element of an array. So, if you had int taco[10] as an array in a program of yours, taco would point to your array.

If we know how to pass the function the address of the array, the problem is: How do we declare that we're passing the array to a function? There are actually two ways to do it, we'll start with the first. Here's how it looks to pass an array of ints:

void eat_at_joes(int guacamole[])

Take a look at how the array of ints is defined. Notice that there is no number between the brackets -- it's just []. Why is that? Well, using this syntax, you can allow ANY size array of ints to be passed to the function. Let's try passing an array in a program:

#include <iostream.h>

void eat_at_joes(int guacamole[])
{
    guacamole[0] = 1;
    guacamole[1] = 2;
    guacamole[2] = 3;
}

void main(void)
{
   int taco[3];
   int nacho[2];

   eat_at_joes(taco);
   eat_at_joes(nacho);
}

Notice any problems with the example? Take a look at the first call. First, it defines nacho and taco, both arrays of ints. Then it passes "taco", which is actually the address of the array taco, to eat_at_joes. Baving been passed the address of where the array is, it can now access it directly.

Did you notice that the second array, nachos, has only 2 elements? What do you think happens when you pass it to eat_at_joes? The answer, unfortunately, is that it will eventually try to access the third element of the array, which doesn't actually exist. Since arrays are just many variables one after another inside your computer's memory, it will try and access the variable after the second element of the array, which of course doesn't exist.

To add insult to injury, the function might run and compile completely flawlessly. Surprised? Well, remember that C++ is based on C, who was of course famous for letting you read or write into a computer's memory without actually knowing if you actually have variables there. Be careful of cases like this, because if you don't catch them, your program will either:

  • Run flawlessly, and you will be happy
  • Crash and drive you crazy trying to find the bug
The problem where you are accessing memory that you shouldn't be is sometimes referred to as a "segmentation violation" or a "memory leak" -- either way, it spells trouble. So how can we make sure this never happens? Well, in this example program, we can add a second parameter that tells it how big the array is. As long as we never use more of an array than it says, and always pass the right size, we should be pretty good! Here's the new program with the second parameter:

#include <iostream.h>

void eat_at_joes(int guacamole[], int size)
{
    if (size > 0)
       guacamole[0] = 1;
    if (size > 1)
       guacamole[1] = 2;
    if (size > 2)
       guacamole[2] = 3;
}

void main(void)
{
   int taco[3];
   int nacho[2];

   eat_at_joes(taco, 3);
   eat_at_joes(nacho, 2);
}

By adding the second parameter to eat_at_joes, we can check to make sure we don't do any damage.


Table of Contents

Parameter Passing | Passing Pointers | Passing Arrays | Reference Variables
Exercises