
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Why doesn't C++ 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.
- Related Articles
- Why doesn't JavaScript support multithreading?
- Why doesn't MySQL support millisecond / microsecond precision?
- Python3 - Why loop doesn't work?
- Why milk doesn't contain Vitamin C?
- Why doesn't JavaScript have a goto statement?
- Why Black hole doesn\'t emit the light?
- Why dispersion doesn't occur in glass slab?
- Why does C++ require a cast for malloc() but C doesn't?
- Why final variable doesn't require initialization in main method in java?
- How a magnet attracts iron ? And it doesn't attract insulator. Why?
- Why subclass doesn't inherit the private instance variables of superclass in Java?
- Metal react with water but why non-metal doesn't react with water?
- If air pressure acting on chair then why it doesn't get crushed?
- Why doesn't height: 100% work to expand divs to the screen height?
- How to deal with Internet Explorer and addEventListener problem "Object doesn't support this property or method" in JavaScript?

Advertisements