

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Passing an array to a C++ function
C++ does not allow to pass an entire array as an argument to a function. However, You can pass a pointer to an array by specifying the array's name without an index.
If you want to pass a single-dimension array as an argument in a function, you would have to declare function formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.
There are 3 ways to pass an array to a function −
Formal parameters as a pointer
void myFunction(int *param) { // Do something }
Formal parameters as a sized array
void myFunction(int param[10]) { // Do something }
Formal parameters as an unsized array
void myFunction(int param[]) { // Do something }
Example
You can use it as follows −
#include<iostream> using namespace std; void arrayAccept(int arr[]) { cout << "first element is: " << arr[0]; } int main() { int arr[2]; arr[0] = 0; arr[1] = 1; arrayAccept(arr); return 0; }
Output
This will give the output −
first element of array is 0
- Related Questions & Answers
- Passing a 2D array to a C++ function
- Passing two dimensional array to a C++ function
- Passing an array to a query using WHERE clause in MySQL?
- Passing Arrays to Function in C++
- Is there an elegant way of passing an object of parameters into a function?
- Passing a function as a callback in JavaScript
- Passing array to method in Java
- What MySQL returns on passing an invalid string as an argument to STR_TO_DATE() function?
- Convert an array of datetimes into an array of strings passing units in Python
- Passing unknown number of arguments to a function in Javascript
- Convert an array of datetimes into an array of strings passing minutes datetime unit in Python
- Convert an array of datetimes into an array of strings passing hour datetime unit in Python
- C++ Program to Add Complex Numbers by Passing Structure to a Function
- How to return an array from a function in C++?
- C++ Program to Multiply two Matrices by Passing Matrix to Function
Advertisements