investigated the options to return an array from a function

This commit is contained in:
2021-10-16 09:19:44 +02:00
parent d3138a4b13
commit d6287d6002
10 changed files with 127 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <iterator>
#include <iostream>
using namespace std;
void ret_array(int *ret, int amount){
for(int i=0;i<amount;i++)
//*(ret+i)=+1;
ret[i]=i*2; // Simpler because arrays are already pointers: startloc + typesize*<index>
}
int* ret_array2(){
static const int amount = 10;
static int ret[amount];
for(int i=0;i<amount;i++)
*(ret+1*i)=i*3;
return ret;
}
int main(){
int new_array[10];
ret_array(new_array,10); //Array is already a pointer, pointing to the first element;
for(int i:new_array)
cout <<"Number "<<i<<endl;
int *arr2= ret_array2();
for(int i=0;i<10;i++)
cout <<"Number "<<arr2[i]<<endl;
return 0;
}
Binary file not shown.