investigated the options to return an array from a function

This commit is contained in:
Jean Jacques Avril 2021-10-16 09:19:44 +02:00
parent d3138a4b13
commit d6287d6002
10 changed files with 127 additions and 0 deletions

29
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,29 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"iterator": "cpp"
}
}

28
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

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.

View File

@ -0,0 +1,11 @@
#include <iostream>
using namespace std;
int main(){
for(int i=1; i<=100;i++){
if(i%2!=0) continue;
cout<<i<<endl;
}
}

View File

@ -0,0 +1,24 @@
#include <iostream>
#include <string>
using namespace std;
struct Tier{
uint8_t id;
string name;
bool operator == (const Tier& cmp) const {return id==cmp.id;}
bool operator < (const Tier& cmp) const {return id-cmp.id;}
string to_string(){return "ID: "+id+(name.length()>0?("Name: "+name):"")+"\n";}
};
int main(){
Tier tiere[50];
cout<<"Erstelle Liste\n";
for(int i = 0;i<50;i++){
cout<<"Erstelle index "<<i<<endl;
tiere[i].id=i+1;
}
for(Tier tier:tiere){
cout<<tier.to_string()<<endl;
}
system("pause");
}

Binary file not shown.