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 :: delete a node from binery search tree c++ 
Cpp :: find max value in array c++ 
Cpp :: cpp create multidimensional vector 
Cpp :: memset in c++ 
Cpp :: c++ sieve of eratosthenes 
Cpp :: c++ cout colored output xcode 
Cpp :: push_back struct c++ 
Cpp :: overload stream extract cpp 
Cpp :: ray sphere intersection equation 
Cpp :: convert integer to string c++ 
Cpp :: increment c++ 
Cpp :: coordinate in 1d array 
Cpp :: c++ default parameters 
Cpp :: struct and pointer c++ 
Cpp :: how to split a string in c++ 
Cpp :: declare nullptr c++ 
Cpp :: convert unsigned long to string c++ 
Cpp :: back() in c++ 
Cpp :: c++ lambda 
Cpp :: sorting using comparator in c++ 
Cpp :: log in c++ 
Cpp :: print vector c++ 
Cpp :: Setting a number of decimals on a float on C++ 
Cpp :: how to empty string c++ 
Cpp :: iterate over map c++ 
Cpp :: Disabling console exit button c++ 
Cpp :: c function as paramter 
Cpp :: c++ Least prime factor of numbers till n 
Cpp :: best time to buy and sell stock leetcode solution 
Cpp :: C++ String Compare Example 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =