Using a pointer to return an array from a function?

Mar 24, 2009 at 7:06pm
Hello,

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.

I have came out with the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main()
{
    //prototypes 
    float input();
    //float calculation(float,float);
	//float output(float,float);
	
	cout<<input();

  return EXIT_SUCCESS;
}
  
float input()
{
   float result[5];

   result[0]=2.4;
   result[1]=8.8;

 return *result;

}


When I run this code I get 2.4, which is the first element of the array.


How can I access the rest of the elements of the array in the main function?

I will apreciate any help.

Thank you
Angel
Mar 24, 2009 at 7:28pm
You have to make a function with a parameter.

Eg:

1
2
3
4
5
6
7
8
9
float input(const int& pos) const
{
    float result[5];

    result[0] = 2.4;
    result[1] = 8.8;

    return result[pos];
}



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.
Mar 24, 2009 at 7:41pm
In the program that you made you have assigned values to an array inside the function input.

however the return value of this function is only a single float.

with :

return *result you only pass the first value of array result back to the caller.

the rest of the array gets discarded after closing the function.

I made a small example of how I usually fix these problems, however, its not realy a perfomance monster....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// float_array.cpp : main project file.
#include "stdafx.h"
#include <iostream>

using namespace 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;
}
Mar 24, 2009 at 7:57pm
R0N sorry for telling you this but you're giving an bad example, array<System::String ^> ^args will not work useless you are in dotnet environment.
Mar 24, 2009 at 8:08pm
Indeed, I made a better example now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// float_array.cpp : main project file.
#include "stdafx.h"  // Visual Studio specific.
#include <iostream>

using namespace 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.

regards, Ronnie van Aarle.
Mar 24, 2009 at 8:26pm
Thank you for your help!

I got it to work like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float input(const int& array_member)
{
    float result[5];

    result[0] = 2.4;
    result[1] = 8.8;
    result[2] = 3.1;
    result[3] = 6.7;
    result[4] = 9.1;
	
    return result[array_member];
}
int main()
{
	cout<<input(4);

  return EXIT_SUCCESS;
}


Now I can call any element in the array.

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?

I am confused.

Mar 24, 2009 at 11:35pm
All the input function is doing is camouflaging a normal array index.

input(4) is the same as initializing the array result in the main function and accessing the index #4.

1
2
3
4
5
6
7
8
int main()
{
  float result[5] = { 2.4, 8.8, 3.1, 6.7, 9.1 };

  cout << result[4] << endl;

  return 0;
}


You could do something like this:
1
2
3
4
5
6
void input(int& array_member)
{
  // init the result array here

  array_member = result[array_member];
}


This could take the array index and then store the value into the reference variable.

You would have to pass in a variable because the function needs an address to send the data back to. If it is a number, there is no address.

Example:
1
2
3
4
5
6
7
8
//input function
int main()
{
  int array_element = 2;
  input(array_element);
  cout << array_element << endl;
  return 0;
}

This would print out the value at index 2 or 3.1.
Last edited on Mar 24, 2009 at 11:42pm
Topic archived. No new replies allowed.