
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Why C++ Doesn't Support Functions Returning Arrays
Let us consider this following program,
#include <iostream> using namespace std; int* Array() { int a[100]; a[0] = 7; a[1] = 6; a[2] = 4; a[3] = 3; return a; } int main() { int* p = Array(); cout << p[0] << " " << p[1]<<" "<<p[2]<<" "<<p[3]; return 0; }
In this program,We get warning as,
Output
In function 'int* Array()': warning: address of local variable 'a' returned [-Wreturn-local-addr] int a[100];
We return the address of local variable but this is not possible as local variable may not exist in memory after the end of function call. So, C++ doesn’t support function returning arrays.
Advertisements