C++ Can';s似乎使用返回指针double来正确地打印cout的值
我不能用cout打印以屏蔽返回的double指针指向的值,如果我使用printf而不是正确打印的值,如果我使用我在函数中声明的double指针,cout它也可以正确打印,如果我调试,我会看到double指针正确返回它的值(请注意,我指的是静态的而不是本地的),无法理解这里的代码出了什么问题(在Visual Studio 2017社区版中成功编译)C++ Can';s似乎使用返回指针double来正确地打印cout的值,c++,pointers,printf,cout,C++,Pointers,Printf,Cout,我不能用cout打印以屏蔽返回的double指针指向的值,如果我使用printf而不是正确打印的值,如果我使用我在函数中声明的double指针,cout它也可以正确打印,如果我调试,我会看到double指针正确返回它的值(请注意,我指的是静态的而不是本地的),无法理解这里的代码出了什么问题(在Visual Studio 2017社区版中成功编译) #包括“stdafx.h” #包括 使用名称空间std; 常数双倍增值税=0.17; 双倍*增值税(静态双倍和) { 总和=总和+(总和*增值税);
#包括“stdafx.h”
#包括
使用名称空间std;
常数双倍增值税=0.17;
双倍*增值税(静态双倍和)
{
总和=总和+(总和*增值税);
回报与总和;
}
int main()
{
双倍总数=120;
双倍*含增值税总额;
cout>total;
总增值税=加增值税(总);
printf(“含增值税的总额为:%f\n”,*含增值税的总额);
cout您不能将函数参数声明为static
,您也尝试返回局部变量上的指针,但当函数离开其作用域时,其局部变量不再存在。如果要在double上获取有效指针,则需要通过引用传递sum:
double * addVAT(double& sum) // takes reference
{
sum = sum + (sum*VAT);
return ∑
}
但这个double*addVAT(double&sum)函数将更改传递给它的变量,在这里,在调用方法后,将更改total
int main()
{
//...
totalWithVat = addVAT(total);// in addVAT sum will referenced to total from main's scope, so it will return pointer to total
std::cout << total; // will print the same as totalWithVat
std::cout << totalWithVat; // will print the same as total, totalWithVat points to total
//...
}
只需使用常规功能:
double totalWithVAT( double sum )
{
const double vat = 1.17;
return sum * vat;
}
std::cout << "The total with VAT is:" << totalWithVAT( total ) << std::endl;
含增值税双倍合计(双倍合计)
{
常数双倍增值税=1.17;
退货金额*增值税;
}
std::cout让我们看看您的addVat
函数
double * addVAT(static double sum)
{
sum = sum + (sum*VAT);
return ∑
}
您需要从参数列表中删除static
。至少对于Linux上的GCC,这会导致编译错误。当我这样做时,我会得到
<source>: In function 'double* addVAT(double)':
<source>:8:24: warning: address of local variable 'sum' returned
[-Wreturn-local-addr]
double * addVAT(double sum)
~~~~~~~^~~
Compiler returned: 0
或者您可以有两个值,而不必担心指针/引用
double addVat(double sum)
{
return sum + sum*VAT;
}
double total = 1.0;
double totalWithVat = addVat(total);
// totalWithVat now ~1.17
修复方法是从参数中删除static并传递指针,而不是static,奇怪的是,旧代码编译并成功地使用printf,这是正确的代码,在cout和printf中都可以使用:
#include "stdafx.h"
#include <iostream>
using namespace std;
const double VAT=0.17;
double * addVAT(double *sum)
{
*sum = *sum + (*sum*VAT);
return sum;
}
int main()
{
double varTotal = 0;
double *total;
total = &varTotal;
double *totalWithVat;
cout << "Type the total before VAT:";
cin >> *total;
totalWithVat = addVAT(total);
printf("The total with VAT is: %f \n", *totalWithVat);
cout << *totalWithVat << endl;
}
#包括“stdafx.h”
#包括
使用名称空间std;
常数双倍增值税=0.17;
双倍*增值税(双倍*总和)
{
*总和=*总和+(*总和*增值税);
回报金额;
}
int main()
{
双变量总计=0;
双倍*总计;
总计=&varTotal;
双倍*含增值税总额;
cout>*总计;
总增值税=加增值税(总);
printf(“含增值税的总额为:%f\n”,*含增值税的总额);
我从VC++得到的数据不可能重复,这应该是一个错误。你的代码没有你认为的那样做。addVAT声明中的这个静态是什么。我不认为它编译后不会编译,因为static double sum
对于一个参数来说是无效的存储类。addVAT例程返回一个悬空指针。De引用悬空指针是未定义的行为。未定义的行为可以做正确的事情,并且可以给出奇怪的输出。我不认为OP想要修改total
@Slava是的,可能最好使用函数而不修改total。只是他使用了引用和指针,所以我想他想知道如何使我我会在我的答案中添加这些信息来改进它。ThanksI使用了static,因为据我所知,static会使其他局部变量在函数终止后存活很长时间,它应该保存在堆上而不是堆栈上,我错了吗?@Aviad430是的,静态变量只有一个实例,并且一直存在到程序结束,但您不能将其用作参数。静态变量只初始化一次,因此,当您将值传递给函数时,以后无法对其进行初始化,请不要将静态用作参数。顺便问一下,我的gcc在静态参数上抛出错误,您没有错误吗?没有,代码在Visual Studio 2017 community edition中已成功编译。如果变量eclared as static假设将其值保存在堆上而不是堆栈上,一旦函数终止,变量将保留在堆上,如果没有static关键字,变量将确实是一个局部变量,并在函数完成执行后终止。您所说的是真的,但函数参数列表不是您需要的位置之一可以使用静态
。它可以在文件范围、函数范围和类范围内使用。在这些情况下,内存不在堆上,而是另一个内存区域(堆或堆栈)。
void addVat(double& sum)
{
sum += sum*VAT;
}
double total = 1.0;
addVat(total);
// total now ~1.17
double addVat(double sum)
{
return sum + sum*VAT;
}
double total = 1.0;
double totalWithVat = addVat(total);
// totalWithVat now ~1.17
#include "stdafx.h"
#include <iostream>
using namespace std;
const double VAT=0.17;
double * addVAT(double *sum)
{
*sum = *sum + (*sum*VAT);
return sum;
}
int main()
{
double varTotal = 0;
double *total;
total = &varTotal;
double *totalWithVat;
cout << "Type the total before VAT:";
cin >> *total;
totalWithVat = addVAT(total);
printf("The total with VAT is: %f \n", *totalWithVat);
cout << *totalWithVat << endl;
}
#include "stdafx.h"
#include <iostream>
using namespace std;
const double VAT = 0.17;
double * addVAT(double sum)
{
static double sumstatic = sum;
sumstatic = sumstatic + (sumstatic*VAT);
return &sumstatic;
}
int main()
{
double varTotal = 0;
double total;
double *totalWithVat;
cout << "Type the total before VAT:";
cin >> total;
totalWithVat = addVAT(total);
printf("The total with VAT is: %f \n", *totalWithVat);
cout << "The total with VAT is: " << *totalWithVat << endl;
}