function returns pointer to array

100 views Asked by At

i wrote this c++ code to make a function returns a pointer to an array of double, in such way i use it as rvalue. i got a weird error message because i couldn't understand what's wrong with it. here is the code with the error message

#include <iostream>
using std::cout;
using std::endl;

double* fct_returns_ptr(double, int); // function prototype

int main(void)
{
    double test_array[] = { 3.0, 10.0, 1.5, 15.0 }; // test value
    int len = (sizeof test_array)/(sizeof test_array[0]);
    //double* ptr_result = new double(0.0); //[len]  pointer to result
    double* ptr_result = new double[len]; // (0.0) pointer to result
    ptr_result = fct_returns_ptr(test_array, len);

    for (int i=0; i<len; i++)
        cout << endl << "Result = " << *(ptr_result+i); // display result

    cout << endl;
    delete [] ptr_result; // free the memory
    return 0;
}

// function definition
double* fct_returns_ptr(double data[], int length)
{
    double* result = new double(0.0);
    for (int i=0; i<length; i++)
        *(result+i) = 3.0*data[i];
    return result;
}
/*
C:\Users\laptop\Desktop\C_CPP>cl /Tp returns_ptr.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.40219.01 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

returns_ptr.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : warning C4530: C++ 
exception handler used, but unwind semantics are not enabled. Specify /EHsc
returns_ptr.cpp(13) : error C2664: 'returns_ptr' : cannot convert parameter 1 from 'double [4]' to 'double'
        There is no context in which this conversion is possible
*/
2

There are 2 answers

1
sonus21 On BEST ANSWER
#include <iostream>
using namespace std;

double* fct_returns_ptr(double  *, int); // function prototype

int main(void)
{
    double test_array[] = { 3.0, 10.0, 1.5, 15.0 }; // test value
    int len = (sizeof test_array)/(sizeof test_array[0]);
    //double* ptr_result = new double(0.0); //[len]  pointer to result
    double* ptr_result = new double[ len ] ;
    ptr_result = fct_returns_ptr(test_array, len);

    for (int i=0; i<len; i++)
        cout << endl << "Result = " << *(ptr_result+i); // display result

    cout << endl;
    delete [] ptr_result; // free the memory
    return 0;
}

// function definition
double* fct_returns_ptr(double *data, int length)
{
    double* result = new double[length];
    for (int i=0; i<length; i++)
        *(result+i) = 3.0*data[i];
    return result;
}

Try this , What's the problem was there ? Your function prototype and signature didn't match .

0
NathanOliver On

In fct_returns_ptr() the line double* result = new double(0.0); does not create an array of doubles but creates a double pointer initilized to 0.0. I suspect that you meant to do:

double* result = new double[length];

You also do not need

double* ptr_result = new double[len]; // (0.0) pointer to result
ptr_result = fct_returns_ptr(test_array, len);

in main() as you are creating the array in the function. You can change it to:

double* ptr_result = fct_returns_ptr(test_array, len);