codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

26 / 87 topics
25Arrays (1D and Multidimensional)26Passing Array to a Function27Strings & the C++ String Class
Tutorials/C++ Programming/Passing Array to a Function
⚡C++ Programming

Passing Array to a Function

Updated 2026-05-12
30 min read

Passing Array to a Function

In this tutorial, we will explore how to pass arrays to functions in C++. We'll cover various aspects such as passing the array size, returning arrays from functions, and understanding the relationship between arrays and pointers. This knowledge is crucial for effective data manipulation and function design in C++.

Introduction

Arrays are fundamental data structures in C++, used to store collections of elements of the same type. Passing arrays to functions allows you to perform operations on them without duplicating the entire array. Understanding how arrays behave when passed to functions, including their size and relationship with pointers, is essential for writing efficient and maintainable code.

Passing Arrays to Functions

When an array is passed to a function in C++, it decays into a pointer to its first element. This means that the function receives a pointer rather than the entire array. As a result, you cannot determine the size of the array within the function unless you explicitly pass it as a parameter.

Example 1: Passing an Array Without Size

In this example, we will pass an array to a function without specifying its size. The function will only receive a pointer to the first element.

pass_array.cpp
1#include <iostream>
2using namespace std;
3
4void printArray(int arr[], int size) {
5 for (int i = 0; i < size; ++i) {
6 cout << arr[i] << " ";
7 }
8 cout << endl;
9}
10
11int main() {
12 int numbers[] = {1, 2, 3, 4, 5};
13 int size = sizeof(numbers) / sizeof(numbers[0]);
14 printArray(numbers, size);
15 return 0;
16}
Output
1 2 3 4 5

Explanation

  • The printArray function takes an array (int arr[]) and its size (int size) as parameters.
  • Inside the function, we use a loop to iterate over the elements of the array.
  • In the main function, we define an array numbers and calculate its size using sizeof.
  • We then call printArray, passing the array and its size.

Common Mistake

A common mistake is trying to determine the size of the array within the function without passing it as a parameter. Since arrays decay into pointers when passed to functions, you cannot use sizeof inside the function to get the number of elements.

Passing Array Size Parameter

Passing the size of the array explicitly is crucial for operations that depend on knowing how many elements are in the array. As shown in the previous example, always pass the size as a separate parameter.

Example 2: Modifying Elements in an Array

In this example, we will modify the elements of an array inside a function and observe the changes in the main function.

modify_array.cpp
1#include <iostream>
2using namespace std;
3
4void incrementArray(int arr[], int size) {
5 for (int i = 0; i < size; ++i) {
6 arr[i] += 1;
7 }
8}
9
10int main() {
11 int numbers[] = {1, 2, 3, 4, 5};
12 int size = sizeof(numbers) / sizeof(numbers[0]);
13 incrementArray(numbers, size);
14
15 for (int i = 0; i < size; ++i) {
16 cout << numbers[i] << " ";
17 }
18 cout << endl;
19 return 0;
20}
Output
2 3 4 5 6

Explanation

  • The incrementArray function takes an array and its size as parameters.
  • Inside the function, we increment each element of the array by 1.
  • In the main function, after calling incrementArray, we print the modified array to verify that the changes are reflected.

Returning Arrays from Functions

Returning arrays from functions is a bit tricky because C++ does not support returning entire arrays directly. Instead, you can return pointers or use dynamic memory allocation. However, using references or standard containers like std::vector is generally more efficient and safer.

Example 3: Returning a New Array Using Dynamic Memory Allocation

In this example, we will create a new array inside a function and return a pointer to it.

return_array.cpp
1#include <iostream>
2using namespace std;
3
4int* createArray(int size) {
5 int* arr = new int[size];
6 for (int i = 0; i < size; ++i) {
7 arr[i] = i + 1;
8 }
9 return arr;
10}
11
12int main() {
13 int size = 5;
14 int* numbers = createArray(size);
15
16 for (int i = 0; i < size; ++i) {
17 cout << numbers[i] << " ";
18 }
19 cout << endl;
20
21 // Don't forget to free the allocated memory
22 delete[] numbers;
23 return 0;
24}
Output
1 2 3 4 5

Explanation

  • The createArray function allocates a new array of integers on the heap and initializes it.
  • It returns a pointer to the first element of the allocated array.
  • In the main function, we call createArray, store the returned pointer in numbers, and print the elements.
  • After using the array, it's crucial to free the allocated memory using delete[] to avoid memory leaks.

Tip

Always remember to deallocate dynamically allocated memory to prevent memory leaks. Using smart pointers or standard containers can help manage memory more safely.

Arrays and Pointers Relationship

Understanding the relationship between arrays and pointers is essential for working with arrays in C++. When an array is passed to a function, it decays into a pointer to its first element. This decay happens only when the array is used as a function argument or assigned to a pointer.

Example 4: Using Pointers to Access Array Elements

In this example, we will use pointers to access and modify elements of an array.

array_pointers.cpp
1#include <iostream>
2using namespace std;
3
4void printArray(int* arr, int size) {
5 for (int i = 0; i < size; ++i) {
6 cout << *(arr + i) << " ";
7 }
8 cout << endl;
9}
10
11int main() {
12 int numbers[] = {1, 2, 3, 4, 5};
13 int size = sizeof(numbers) / sizeof(numbers[0]);
14 printArray(numbers, size);
15
16 // Using pointer arithmetic to modify elements
17 for (int i = 0; i < size; ++i) {
18 *(numbers + i) *= 2;
19 }
20
21 printArray(numbers, size);
22 return 0;
23}
Output
1 2 3 4 5 
2 4 6 8 10

Explanation

  • The printArray function takes a pointer to an integer and its size as parameters.
  • Inside the function, we use pointer arithmetic (*(arr + i)) to access elements of the array.
  • In the main function, we also modify the elements using pointer arithmetic.

Practical Example: Sorting an Array

Let's put everything together in a practical example. We'll create a function that sorts an array and returns it.

sort_array.cpp
1#include <iostream>
2using namespace std;
3
4void sortArray(int arr[], int size) {
5 for (int i = 0; i < size - 1; ++i) {
6 for (int j = 0; j < size - i - 1; ++j) {
7 if (arr[j] > arr[j + 1]) {
8 // Swap elements
9 int temp = arr[j];
10 arr[j] = arr[j + 1];
11 arr[j + 1] = temp;
12 }
13 }
14 }
15}
16
17int main() {
18 int numbers[] = {5, 2, 9, 1, 5};
19 int size = sizeof(numbers) / sizeof(numbers[0]);
20
21 cout << "Original array: ";
22 for (int i = 0; i < size; ++i) {
23 cout << numbers[i] << " ";
24 }
25 cout << endl;
26
27 sortArray(numbers, size);
28
29 cout << "Sorted array: ";
30 for (int i = 0; i < size; ++i) {
31 cout << numbers[i] << " ";
32 }
33 cout << endl;
34
35 return 0;
36}
Output
Original array: 5 2 9 1 5 
Sorted array: 1 2 5 5 9

Explanation

  • The sortArray function implements a simple bubble sort algorithm to sort the array.
  • In the main function, we define an array, print it, call sortArray, and then print the sorted array.

Summary

ConceptDescription
Passing ArraysArrays decay into pointers when passed to functions.
Array Size ParameterAlways pass the size of the array as a separate parameter.
Returning ArraysUse dynamic memory allocation or standard containers for returning arrays.
Arrays and PointersUnderstanding the relationship helps in accessing and modifying elements.

What's Next?

In the next tutorial, we will explore strings and the C++ std::string class. Strings are another essential data structure in programming, and mastering them will enhance your ability to handle text data efficiently.

Stay tuned for more tutorials on C++ programming!


PreviousArrays (1D and Multidimensional)Next Strings & the C++ String Class

Recommended Gear

Arrays (1D and Multidimensional)Strings & the C++ String Class