Passing Arrays to Functions in C Plus Plus

This tutorial illustrates techniques for passing arrays to functions in C++ (C plus plus). Three modules are provided; main.cpp, Enumerate.cpp, and Enumerate.h.

C++ (C Plus Plus) passes an array to a function by reference. In other words, the address of the first element of the array is passed. The contents of the array is not passed. By definition, any C++ array must be stored contiguously in memory. Therefore, knowing the address of the first element of the array is sufficient to traverse the entire array. However, the length of the array is not available. This is problematic unless a second argument is passed or an array delimiter is assumed. In the case of character strings, the delimiter is typically assumed to be a null terminator. In the case of arrays consisting of other data types, a length argument is usually necessary.

The null terminator is typically applied as the array delimiter for a character string. Referring to the ASCII chart, the first entry in that chart is usually assumed to the the null terminator. Programmers use several representations in code; 0 or '\0' are common. The former is not strictly correct, the latter is mapped by the compiler to the proper value. In the vast majority of C++ (C Plus Plus) programs, a simple 0 suffices. However, some compilers are configurable to the extent that an alternate value for the null terminator can be specified. In these relatively infrequent cases, unexpected results will arise if '\0' is not used in code.

Referring to Enumerate.cpp; we have configured our function to accept one argument.

void Enumerate(char *buff)

The argument, char *buff,  is the address of a char data item. Many programmers refer to this construct as "a pointer to a char" or "a pointer to a character string." The latter is not strictly correct. As written, our function assumes that other char data items are contiguous to the initial char, but that assumption is not encoded in the definition of the function interface.

By incrementing the pointer we can traverse the contents of the array. This logic falls apart if the calling function (in this example that's the main() ) does not provide an array to traverse.

/******************************************************************************
 * Passing Arrays to Functions                                  Enumerate.cpp *
 *                                                                            *
 * Author: nicomp                                                             *
 *                                                                            *
 * Abstract: This project illustrates techniques for passing arrays to        *
 *           functions as arguments and returning them in the return statement*
 ******************************************************************************/

#include <iostream>
#include "Enumerate.h"
using namespace std;
void Enumerate(char *buff)
{
	// All we know about buff is: it is the address of a character.
	// We have to assume the rest.

	// We can enumerate it using array notation, but we do not know when to stop.
	//	There is no implicit delimiter unless we choose to enforce one.
	// In this loop we arbitrarily choose to process 5 characters, then stop.
	cout << "<";
	for (int i = 0; i < 5; i++) cout << buff[i];	// This might work, or not.
	cout << ">";

	// We can use a null terminator as a delimiter.
	// If the null terminator is present, this works well.
	// If the null terminator is not present, we will probably see
	//  a memory exception.
	cout << "\n<" << buff << ">\n";
}
/******************************************************************************
 * Passing Arrays to Functions                                       main.cpp *
 *                                                                            *
 * Author: nicomp                                                             *
 *                                                                            *
 *                                                                            *
 * Abstract: This project illustrates techniques for passing arrays to        *
 *           functions as arguments and returning them in the return statement*
 *                                                                            *
 ******************************************************************************/
#include <iostream>
#include "Enumerate.h"

using namespace std;

void main()
{
	char *myString = "Hello World";
	Enumerate(myString);

}
#pragma once
/******************************************************************************
 * Passing Arrays to Functions                                    Enumerate.h *
 *                                                                            *
 * Author: nicomp                                                             *
 *                                                                            *
 * When an array is passed to a function, the function only sees the          *
 *  address of element[0]. The other elements are addressable from there.     *
 *                                                                            *
 * The rule of arrays: the name of an array is the address of the first       *
 *  element in the array. All other elements are addressable from there.      *
 ******************************************************************************/

void Enumerate(char *buff);
Output of program
Output of program

More by this Author


Comments 2 comments

psychicdog.net profile image

psychicdog.net 7 years ago

Just for the record Nicomp, when I read this, about half way down there is a Google Ad for a Function Center for "Your Complete Wedding package". On the heels of this ad is your line stating, "we have configured our function to accept one argument." I couldn't resist reporting this hilarious juxtaposition!


nicomp profile image

nicomp 7 years ago from Ohio, USA Author

LOL. The ads are funny when they are slightly out of context. Thanks!

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working