C++ 数组变量返回意外值

C++ 数组变量返回意外值,c++,arrays,dynamic-arrays,C++,Arrays,Dynamic Arrays,我试图将数组传递给函数(*getcreditcurve)。我希望函数(*getcreditcurve)返回一个数组。Main函数应向函数(*getcreditcurve)发送多个这样的数组,指针函数应使用指针函数(*getcreditcurve)中给出的逻辑将不同数组的数组返回给Main函数。我没有得到错误,但我没有得到正确的值。我期望I+1为3*0.0039=0.0117,I+2为4*0.0060=0.0024,但我在excel输出中得到以下结果 '00D4F844 00D4F84C'

我试图将数组传递给函数(*getcreditcurve)。我希望函数(*getcreditcurve)返回一个数组。Main函数应向函数(*getcreditcurve)发送多个这样的数组,指针函数应使用指针函数(*getcreditcurve)中给出的逻辑将不同数组的数组返回给Main函数。我没有得到错误,但我没有得到正确的值。我期望I+1为3*0.0039=0.0117,I+2为4*0.0060=0.0024,但我在excel输出中得到以下结果

'00D4F844   00D4F84C'
即使我将打印语句更改为

'print << *(I1+1) << '\t' << *(I2+2) << endl;'
有人能帮忙排除故障吗?很抱歉,我在这个网站上浏览了其他帖子/问题,但没有找到解决这个问题的最简单方法。我将使用这个逻辑来构建其他项目,从而简化问题以解决主要问题

#include<iostream>
#include<cmath>
#include<fstream>
typedef double S1[5];
using namespace std;
double *getcreditcurve(double *);

int main()
{


S1 C1 = { 0.0029, 0.0039, 0.0046, 0.0052, 0.0057 };
S1 C2 = { 0.0020, 0.0050, 0.0060, 0.0070, 0.0080 };

typedef double *issuer;
issuer I1 = getcreditcurve(C1);
issuer I2 = getcreditcurve(C2);


ofstream print;
print.open("result1.xls");
    print << (I1+1) << '\t' << (I2+2) << endl;

    print.close();
    return 0;


}

double *getcreditcurve(S1 ptr)
{
const int cp = 5;
typedef double curve[cp];
curve h;

h[0] = 2 * ptr[0];
h[1] = 3 * ptr[1];
h[2] = 4 * ptr[2];
h[3] = 5 * ptr[3];
h[4] = 6 * ptr[4];

return h;
}
#包括
#包括
#包括
typedef双S1[5];
使用名称空间std;
double*getcreditcurve(double*);
int main()
{
S1 C1={0.0029,0.0039,0.0046,0.0052,0.0057};
S1 C2={0.0020,0.0050,0.0060,0.0070,0.0080};
typedef double*发卡机构;
发卡机构I1=getcreditcurve(C1);
发卡机构I2=getcreditcurve(C2);
流式印刷;
打印打开(“result1.xls”);

打印如果希望
getcreditcurve
返回数组,请尝试以下操作:

const int cp = 5;
typedef double curve[cp];
curve getcreditcurve(S1 ptr) {

但这会给出一个错误<代码>错误:“FoO”声明为返回数组的函数。函数不能返回C数组。但好消息是,如果您完全接受C++,则可以返回<代码> STD::数组< /COD>。

#include<array>
const int cp = 5;
typedef curve std::array<double,cp>;
curve getcreditcurve(S1 ptr) {

事实上,几乎所有C数组的问题都可以通过
std::vector
解决。然后,在特殊情况下,您可以使用
std::array
。但现在请关注
std::vector

不可能从函数返回C数组。您还可以返回其他内容,例如
std::vector
STD::数组< /代码>。你应该考虑重新设计你的应用程序来代替这两个。

但是,如果你真的需要在C++中使用C数组,我建议不要尝试从<代码> GETCuffITReals中返回数组,你将一个额外的数组传递到GeCuffITRealEng/<代码>中,用来存储结果。

void getcreditcurve(double*, double *);
这将解决“范围”问题。调用方(
main
)将在调用
getcreditcurve
之前创建数组,然后将数组传递给
getcreditcurve
。因此,
getcreditcurve
不必负责创建(或销毁)任何对象

double I1[5];
getcreditcurve(I1, C1); // will store its result on `I1`.
如果您真的需要尽快让它工作,这可能是最简单的选择


如果你愿意做一些进一步的修改,你可以做一个更安全的程序。短期目标是废除所有使用
*
(除了用于乘法)

C数组不能通过值传递,特别是它们不能返回。还有其他一些可以做的事情(通过特殊指针),但是最好的C数组是通过引用传递它们。在C++中,引用传递行为是相当一致的,而且工作得很好。

// http://stackoverflow.com/questions/31362360/unexpected-value-return-by-array-variable
#include<iostream>
#include<cmath>
#include<fstream>
typedef double S1[5];
using namespace std;

/* In the following declaration, the two parameters
 * are taken by reference (note the '&').
 * This is almost always the best way to pass arrays.
 *
 * Also, this is a template where N is automatically
 * set to the correct number of parameters. This nice
 * automatic behaviour is possible only because the
 * array is taken by reference.
 *
 * Finally, note that the second reference, for 'input',
 * has 'const'. This is to emphasize that 'input' is for input,
 * that getcreditcurve will not be allowed to modify the input argument.
 */
template<size_t N>
void getcreditcurve(double (&output)[N],const double (&input)[N]);

int main()
{


    /* S1 is the type - array of five doubles */
    /* Here declare and initialize C1 and C2 as two variables
     * of this type */
    S1 C1 = { 0.0029, 0.0039, 0.0046, 0.0052, 0.0057 };
    S1 C2 = { 0.0020, 0.0050, 0.0060, 0.0070, 0.0080 };

    // create the two output arrays first, within main
    S1 I1;
    S1 I2;
    // call getcreditcurve, passing in the output and input arrays
    getcreditcurve(I1,C1);
    getcreditcurve(I2,C2);


    ofstream print;

    /* you can't create Excel(.xls) files in C++ easily
     * Better to just create a .csv file instead
     * csv = comma-separated values
     */
    print.open("result1.csv");
    print << I1[0] << ',' << I2[3] << endl;

    print.close();
    return 0;

}

template<size_t N>
void getcreditcurve(double (&output)[N],const double (&input)[N])
{
    output[0] = 2 * input[0];
    output[1] = 3 * input[1];
    output[2] = 4 * input[2];
    output[3] = 5 * input[3];
    output[4] = 6 * input[4];
}
//http://stackoverflow.com/questions/31362360/unexpected-value-return-by-array-variable
#包括
#包括
#包括
typedef双S1[5];
使用名称空间std;
/*在下面的声明中,两个参数
*通过引用获取(注意“&”)。
*这几乎总是传递数组的最佳方式。
*
*此外,这是一个模板,其中N是自动生成的
*设置为正确的参数数。这很好
*自动行为是可能的,因为
*数组是通过引用获取的。
*
*最后,请注意第二个参考,即“输入”,
*有“const”。这是为了强调“input”代表输入,
*不允许该getcreditcurve修改输入参数。
*/
模板
void getcreditcurve(双精度(&输出)[N],常数双精度(&输入)[N]);
int main()
{
/*S1是五个双精度的类型数组*/
/*这里声明并初始化C1和C2作为两个变量
*这种类型的*/
S1 C1={0.0029,0.0039,0.0046,0.0052,0.0057};
S1 C2={0.0020,0.0050,0.0060,0.0070,0.0080};
//首先在main中创建两个输出数组
s1i1;
s1i2;
//调用getcreditcurve,传入输出和输入数组
GetCredit曲线(I1,C1);
GetCredit曲线(I2,C2);
流式印刷;
*不能在C++中轻松创建Excel(.xLS)文件
*最好只创建一个.csv文件
*csv=逗号分隔的值
*/
打印打开(“result1.csv”);

打印数组不会隐式复制。这就是为什么它们不是很方便,应该用
std::array
替换的原因之一。似乎几乎是重复的…存储指向超出范围的变量的指针。好的,我将尝试vector,但只是确认“typedef curve std::array”或“std::vector getcreditcurve(std::vector ptr)”只是更改getcredit曲线函数。我已经用大量数组编写了我的ain项目。Sp more尝试使用数组,但概念上数组和向量将工作相同(撇开大小问题不谈?您将不得不进行更多更改。基本上,您应该完全避免
*
(当然,乘法除外)你必须写
向量C1{0.0029,0.0039,0.0046,0.0052,0.0057};
才能将C1定义为一个向量。我知道这似乎需要做很多工作,但在C++中最好避免
*
-指针。另外,当你使用“数组”这个词时,你必须非常清楚。你应该说“C数组”当讨论您现在使用的数组类型时,例如char c[5]
,当谈论c++11中改进的数组时,您应该说“
std::array
”。我以后会尽量澄清。大多数人说“array”是指“c array”,但最好始终澄清我看到另一条与之相关的评论“存储指向变量的指针”
double I1[5];
getcreditcurve(I1, C1); // will store its result on `I1`.
// http://stackoverflow.com/questions/31362360/unexpected-value-return-by-array-variable
#include<iostream>
#include<cmath>
#include<fstream>
typedef double S1[5];
using namespace std;

/* In the following declaration, the two parameters
 * are taken by reference (note the '&').
 * This is almost always the best way to pass arrays.
 *
 * Also, this is a template where N is automatically
 * set to the correct number of parameters. This nice
 * automatic behaviour is possible only because the
 * array is taken by reference.
 *
 * Finally, note that the second reference, for 'input',
 * has 'const'. This is to emphasize that 'input' is for input,
 * that getcreditcurve will not be allowed to modify the input argument.
 */
template<size_t N>
void getcreditcurve(double (&output)[N],const double (&input)[N]);

int main()
{


    /* S1 is the type - array of five doubles */
    /* Here declare and initialize C1 and C2 as two variables
     * of this type */
    S1 C1 = { 0.0029, 0.0039, 0.0046, 0.0052, 0.0057 };
    S1 C2 = { 0.0020, 0.0050, 0.0060, 0.0070, 0.0080 };

    // create the two output arrays first, within main
    S1 I1;
    S1 I2;
    // call getcreditcurve, passing in the output and input arrays
    getcreditcurve(I1,C1);
    getcreditcurve(I2,C2);


    ofstream print;

    /* you can't create Excel(.xls) files in C++ easily
     * Better to just create a .csv file instead
     * csv = comma-separated values
     */
    print.open("result1.csv");
    print << I1[0] << ',' << I2[3] << endl;

    print.close();
    return 0;

}

template<size_t N>
void getcreditcurve(double (&output)[N],const double (&input)[N])
{
    output[0] = 2 * input[0];
    output[1] = 3 * input[1];
    output[2] = 4 * input[2];
    output[3] = 5 * input[3];
    output[4] = 6 * input[4];
}