I am new to C++, and eventhough I am experienced with PHP, I am having some difficulties returning an array from a function in C++; It is good to know PHP and C++ differ a lot in this matter.
I have read that in order accomplish the same behavior a pointer must be used.
And you must call it by passing the position you want in the parameter:
cout << input(1) << endl;
Another thing is that if you call input() with a position outside the bounds of the array it may throw a exception or return an unexpected results, and don't declare the function prototype inside main, nobody does that, declare it after the includes.
// float_array.cpp : main project file.
#include "stdafx.h"
#include <iostream>
usingnamespace std;
// first, declare a struct with an array in it.
struct float_array
{
float result[5];
};
int main(array<System::String ^> ^args)
{
// declare a function with the just declared struct as the return type.
float_array setresult();
// now declare a local structure of the same type.
float_array returned_structure;
// asign the local structure with the returnvalue of setresult.
returned_structure = setresult();
// show the output.
for (int x=0;x<5;x++)
{
cout<<"result["<<x<<"] contains: "<<returned_structure.result[x]<<endl;
}
return 0;
}
// this function of type float_array returns a filled float array.
float_array setresult()
{
// declare local_scope. This variable will be discarded when the function exits.
float_array local_scope;
// fill the array with dummy values.
for (int x=0;x<5;x++)
{
local_scope.result[x]=float(x*1.21);
}
// return the assinged float_array.
return local_scope;
}
// float_array.cpp : main project file.
#include "stdafx.h" // Visual Studio specific.
#include <iostream>
usingnamespace std;
// first, declare a struct with an array in it.
struct float_array
{
float result[5];
};
int main(array<System::String ^> ^args) // Visual Studio specific
// use one of below for other compilers as VS
// int main()
// int main(int argc, char* argv[])
{
// declare a void function that takes a pointer of type float array as argument.
void setresult(float_array*);
// dynamically create a new object of type float_array and put the pointer
// that points to it in struct_to_be_assigned.
float_array* struct_to_be_assigned = new float_array;
// now call setresult and pass the pointer to the newly created object to it.
// this prevents two objects of the same type occupying memory.
setresult(struct_to_be_assigned);
// show the output.
for (int x=0;x<5;x++)
{
cout<<"result["<<x<<"] contains: "<<struct_to_be_assigned->result[x]<<endl;
}
delete struct_to_be_assigned;
return 0;
}
// declaration of the function setresult.
void setresult(float_array* struct_to_be_assigned)
{
// fill the array with dummy values.
for (int x=0;x<5;x++)
{
// here we use the dereference operator to get acces to member result,
// of the object pointed to by struct_to_be_assigned.
struct_to_be_assigned->result[x]=float(x*1.21);
}
// no return value, because we operate directly in the object we instantiated.
}
This one uses pointers to a dynamically created memory object of type float_array. Now only one object exists in memory, so its better on memory usage and performance as well.
But for the sake of learning, could someone explain the logic of input function?
I do not understand how it is working; I might have an idea but it's just an assumption:
1.- I call the function with a parameter.
2.- The function gets the parameter's physical address.
3.- The array is executed.
4.- The function returns the value of the given parameter's physical address within the array?