Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

How to Pass Arrays to Functions in C++: A Practical Guide

How to Pass Arrays to Functions in C++

This guide explains how to pass single‑ and multi‑dimensional arrays to functions in C++ using real code examples.

In C++, arrays can be supplied as function arguments, and you can even return them indirectly via pointers. Understanding the mechanics behind array decay to pointers and the impact on memory and performance is essential for writing efficient code.


Syntax for Passing Arrays as Function Parameters

The declaration for a function that accepts an array looks like this:

returnType functionName(dataType arrayName[arraySize]) {
    // function body
}

Example:

int total(int marks[5]) {
    // implementation
}

Here, marks is an int array of five elements. When the function is called, only the array’s name (a pointer to its first element) is passed.


Example 1: Passing a One‑Dimensional Array to a Function

#include <iostream>
using namespace std;

// Declare function to display marks
void display(int m[5]) {
    cout << "Displaying marks:" << endl;
    for (int i = 0; i < 5; ++i) {
        cout << "Student " << i + 1 << ": " << m[i] << endl;
    }
}

int main() {
    int marks[5] = {88, 76, 90, 61, 69};
    display(marks); // Pass array by name
    return 0;
}

Output

Displaying marks:
Student 1: 88
Student 2: 76
Student 3: 90
Student 4: 61
Student 5: 69

Key points:

  1. Calling display(marks); passes the address of marks[0].
  2. Inside the function, int m[5] is treated as int* m;, pointing to the same memory.
  3. Modifying m[i] changes the original marks array, illustrating pass‑by‑reference semantics.

Passing a Multi‑Dimensional Array to a Function

For 2‑D arrays, you must specify the number of columns (the size of the second dimension) so the compiler can calculate element offsets.

Example 2: Passing a Two‑Dimensional Array to a Function

#include <iostream>
using namespace std;

void display(int n[][2]) {
    cout << "Displaying values:" << endl;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 2; ++j) {
            cout << "num[" << i <<
                 "][" << j <<
                 "]: " << n[i][j] << endl;
        }
    }
}

int main() {
    int num[3][2] = {{3, 4}, {9, 5}, {7, 1}};
    display(num);
    return 0;
}

Output

Displaying values:
num[0][0]: 3
num[0][1]: 4
num[1][0]: 9
num[1][1]: 5
num[2][0]: 7
num[2][1]: 1

Notes:

Higher‑dimensional arrays follow the same rule: the size of all dimensions except the first must be specified.


Returning an Array from a Function

Functions cannot return an entire array directly. Instead, they return a pointer to the first element, which may refer to a statically allocated array, a dynamic array, or a temporary object. Care must be taken to avoid returning pointers to local (stack) arrays, which would result in dangling references.

Example (static array):

int* getArray() {
    static int arr[5] = {1, 2, 3, 4, 5};
    return arr; // pointer to first element
}

For dynamic allocation, consider returning a std::vector<T> for safety and automatic memory management.

Further reading on safe array handling in C++ is recommended for production code.

C Language

  1. Mastering C++ Functions: From Basics to Advanced Usage
  2. C++ Function Overloading: A Practical Guide
  3. C++ Default Function Arguments: How They Work & Best Practices
  4. Mastering C++ Arrays: Declaration, Initialization, and Practical Examples
  5. Mastering Multidimensional Arrays in C++: Declaring, Initializing, and Looping
  6. C++ Function Overriding Explained – Practical Examples & Best Practices
  7. C++ Friend Functions and Friend Classes: Mastering Access Control
  8. Mastering C++ Virtual Functions: Concepts, Examples, and the Override Keyword
  9. Understanding Polymorphism in C++: A Practical Guide
  10. Master C++ Web Programming with CGI