How to Pass an Array Pointer as a Function Argument in C++? Last Updated : 06 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a common situation arises where we have to pass an array to a function to perform some processing. This can be done by passing an array pointer as a function argument. In this article, we will learn how to pass an array pointer as a function argument in C++. Passing Array Pointer to Function in C++For passing an array to a function, we can simply pass the array pointer (that stores the memory address of the first element) along with the size of an array to the function as an argument. This approach can also be used for dynamic arrays because when we pass an array as a parameter to a function it decays into a pointer to the first element of the array and we will lose the information about its size. C++ Program for Passing Array Pointer to FunctionThe below example demonstrates the passing of an array pointer as a function argument. C++ // C++ program to pass an array pointer as a function // argument. #include <iostream> using namespace std; // Function to print array elements void printArray(int* arr, int size) { for (int i = 0; i < size; ++i) { cout << arr[i] << " "; } } int main() { int numbers[] = { 1, 2, 3, 4, 5 }; int size = sizeof(numbers) / sizeof(numbers[0]); // Passing array pointer to the function printArray(numbers, size); return 0; } Output1 2 3 4 5 Explanation: In the above example numbers array is created and it is passed as an array pointer along with its size to a function named printArray()that prints all the array elements. Note: In C++, by default when passing the entire array to a function, pointer to its first element is passed. Comment More infoAdvertise with us Next Article How to Pass an Array Pointer as a Function Argument in C++? S sourabhcao9e0 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP-Functions CPP Examples +1 More Practice Tags : CPP Similar Reads How to Pass a 3D Array to a Function in C++? In C++, a 3D array is a multidimensional array that has three dimensions, i.e. it can grow in three directions. In this article, we will learn how to pass a 3D array to a function in C++. Pass a 3D Array to a Function in C++ Just like normal 1-dimensional arrays, we can't pass the array to a functio 2 min read How to Pass an Array into a Lambda Function in C++? In C++, a lambda function, also known as a lambda expression, is a way of defining an anonymous function object right at the location where it is invoked or passed as an argument to a function. In this article, we will learn how to pass an array into a lambda function in C++. Passing an Array into a 2 min read How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use 2 min read How to pass and return a 3-Dimensional Array in C++? In C++ a 3-dimensional array can be implemented in two ways: Using array (static)Using vector (dynamic) Passing a static 3D array in a function: Using pointers while passing the array. Converting it to the equivalent pointer type. char ch[2][2][2];void display(char (*ch)[2][2]) { . . .} Program to p 3 min read How to Return a Pointer from a Function in C++? In C++, we can return a pointer from a function which is useful when we want to return large data structures that cannot be returned by value. However, it must be done carefully to avoid memory leaks, dangling pointers, and other issues related to dynamic memory management. In this article, we will 2 min read How to Find the Length of an Array in C++? In C++, the length of an array is defined as the total number of elements present in the array. In this article, we will learn how to find the length of an array in C++.The simplest way to find the length of an array is by using the sizeof operator. First calculate the size of the array in bytes and 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Convert an Array to a Vector in C++? In this article, we will learn how to convert the given array into vector in C++.The simplest method to convert an array to vector is by using range constructor of vector. Letâs take a look at an example:C++#include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 3, 6, 2, 9}; 3 min read How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read How to Copy a Vector to an Array in C++? In C++, vectors are dynamic arrays that can grow and reduce in size as per requirements. Sometimes, we may need to copy the contents of a vector to the POD array. In this article, we will learn how to copy a vector to an array in C++. Example: Input: myVec = {10,20,30,40,50,60};Output: array: {10,20 2 min read Like