mirror of
https://inf-git.fh-rosenheim.de/studavrije7683/cplusplus-training.git
synced 2026-06-29 01:23:21 +00:00
investigated the options to return an array from a function
This commit is contained in:
@@ -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.
Reference in New Issue
Block a user