小编M.I*_*oan的帖子

如果参数传递两次,会发生什么?一次通过价值和一次参考?它会被修改吗?

#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("BAC.TXT");
void eval(int a, int b, int &rez)
{
    rez = a + b;
}
int main()
{
    int nr;
    int s;
    fin >> s;
    while (fin >> nr)
        eval(s, nr, s);
    cout << s << '\n';
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以我有这段代码片段.我正在从文件中读取数字并使用名为"eval"的给定函数跟踪它们的总和.我知道将参数传递两次(在这样的给定实例中)而不是使用另一个变量(不确定,但是,如果它是坏代码,在我的情况下)可能被认为是错误的代码.我的问题是:它会改变变量的值s吗?再次,我通过价值和一次参考传递一次!我已经在我的电脑上编写了代码,它确实改变了它的价值s.现在我的问题是:为什么?如果我以正确的方式提出这个问题:"在后台"会发生什么?

c++

4
推荐指数
1
解决办法
85
查看次数

Why can I not increment the value of an int variable by using "++" when working with pointers?

I am trying to increment the value of an element inside an array, using a pointer storing that element's address.

When I write it as " p++; " it does not work, whereas if I write it as *p=*p+1 it works.

*p=*p+1; // this increments the value
*p++; //while this does not
Run Code Online (Sandbox Code Playgroud)

I am expecting both ways to work

c++ pointers increment

0
推荐指数
1
解决办法
63
查看次数

标签 统计

c++ ×2

increment ×1

pointers ×1