在函数改变事物的价值 [英] Changing the value of something in a function

查看:125
本文介绍了在函数改变事物的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个测试案例更大的东西,这就是为什么它是书面的方式是。我如何才能让code这一点,所以,一个价值会不断递增?在项目中,我称之为分析从一个文件中的一行功能。我需要设置一个结构为特定值的函数调用(函数的参数在主函数中初始化,如下面的code)。

This is a test case for something larger, which is why it is written the way is. How could I make this bit of code so that a's value would keep incrementing? In my project I call a function that parses a line from a file. I need to set values of a struct to certain values that were set in the function call (the parameters of the function were initialized in the main function, like the code below).

int increment(int a)
{
    a++;
    return 0;
}
int main()
{
    int a =0;
    int b =0;
    while( b<5){
        increment(a);
        b++;
        cout << "a is: " << a << ". And b is: " << b << "\n";
    }
    system("PAUSE");
}

感谢。

推荐答案

传递其地址递增

void increment(int *a){
  (*a)++;  
}
increment(&a);
//Using the address of operator pass in the address of a as argument

这篇关于在函数改变事物的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆