Search
 
SCRIPT & CODE EXAMPLE
 

CPP

return array from function c++

#include <iostream> 
using namespace std; 
  
int* fun() 
{ 
    int* arr = new int[100]; 
  
    /* Some operations on arr[] */
    arr[0] = 10; 
    arr[1] = 20; 
  
    return arr; 
} 
  
int main() 
{ 
    int* ptr = fun(); 
    cout << ptr[0] << " " << ptr[1]; 
    return 0; 
} 
Comment

cpp return array

int * fillarr(int arr[], int length){
   for (int i = 0; i < length; ++i){
      // arr[i] = ? // do what you want to do here
   }
   return arr;
}

// then where you want to use it.
int main(){
int arr[5];
int *arr2;

arr2 = fillarr(arr, 5);

}
// at this point, arr & arr2 are basically the same, just slightly
// different types.  You can cast arr to a (char*) and it'll be the same.
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ get the line which call a function 
Cpp :: c++ vs g++ 
Cpp :: cpp getter as const 
Cpp :: argument vs parameter coding c++ 
Cpp :: sort array c++ 
Cpp :: c++ 14 for sublime windoes build system 
Cpp :: c++ string find example 
Cpp :: c++ capture screen as pixel array 
Cpp :: priority queue in c++ 
Cpp :: c++ builder 
Cpp :: Reverse words in a given string solution in c++ 
Cpp :: c++ if example 
Cpp :: how to use custom array in c++ 
Cpp :: how to print in new lines in C++ 
Cpp :: hello c++ 
Cpp :: pointer cpp 
Cpp :: c++ write to csv file append 
Cpp :: set size in c++ 
Cpp :: c++ print text 
Cpp :: is anagram c++ 
Cpp :: data types in c++ 
Cpp :: stack class implementation to file unix-style in c++ 
Cpp :: find text in string c++ true false 
Cpp :: max pooling in c++ 
Cpp :: c++ little endian or big endian 
Cpp :: vector::at() || Finding element with given position using vector in C++ 
Cpp :: c++ custom hash 
Cpp :: c++ insert hashmap 
Cpp :: clear map in C++ 
Cpp :: compare function in c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =