C ++ - 通过引用传递数组,奇怪的事情 [英] C++ - Pass by Reference, Weird Thing for Array

查看:201
本文介绍了C ++ - 通过引用传递数组,奇怪的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,C ++使用Ampersand(&)来创建通过引用函数传递。但是我只是测试了这段代码,我想知道为什么第二个函数看起来与第一个函数类似。现在看来第二个函数是通过引用函数传递。

  #include< iostream> 
使用namespace std;
$ b $ void f1(int(& b)[5])
{
b [0] = 1;
}

void f2(int b [5])
{
b [1] = 2;
}

int main()
{
int a [5];
f1(a);
f2(a);

cout<< a< 0 [<< endl;
cout<< a [1]<< endl;
}

输出:

  1 
2

任何人都可以向我解释这个?谢谢。

解决方案

当你将一个数组传递给一个函数时,它退化为一个指针。所以在任何一种情况下,结果都是一样的。



因此,如果 f2 b 是一个指针,它指向数组a从相同的位置开始。还要记住数组索引[ a [i] ]和指针算术和解引用[ *(a + i) ]是同义词。因此,在第二种情况下,您最终会更新传递的原始数组。

现在两个声明之间存在细微差别。在 f1 的情况下,参数 b 是对原始数组 a ,因此 b 仍然是具有源数组的所有特性的同一个数组。在第二个表达式中, b 只是偶然指向数组 a 的位置的指针



考虑到参数完全不同的事实,它们具有不同的行为


  1. 参考变量的Sizeof返回数组的实际大小。
  2. 不能修改函数内的引用参数。


    为了完整起见,这里是一个更新的程序,它会给你更多关于行为不同的想法。

      #include< iostream> 
    使用namespace std;

    void f1(int(& b)[5])
    {
    std :: cout<< sizeof(b)<< std :: endl;
    // b = new int [5]; - 会给编译错误
    b [0] = 1;
    }

    void f2(int b [5])
    {
    std :: cout<< sizeof(b)<<< std :: endl ;
    // b = new int [5]; - 会编译罚款
    b [1] = 2;
    }
    int main()
    {
    int a [5];
    f1(a);
    f2(a);

    cout<< a< 0 [<< endl;
    cout<< a [1]<< endl;
    }


    As far as I know, C++ use Ampersand(&) to make a Pass by Reference Function. But I just tested this code, and I wonder why the second function seems similar with the first function. It seems the second function is a Pass by Reference Function now.

    #include <iostream>
    using namespace std;
    
    void f1(int (&b)[5])
    {
        b[0]=1;
    }
    
    void f2(int b[5])
    {
        b[1]=2;
    }
    
    int main()
    {
        int a[5];
        f1(a);
        f2(a);
    
        cout<<a[0]<<endl;
        cout<<a[1]<<endl;
    }
    

    Output:

    1
    2
    

    Anybody can explain me about this? Thanks.

    解决方案

    When you pass an array to a function, it degenerates to a pointer. So in either case, the results are the same.

    So in case of f2, b is a pointer, which points to the same location the array a starts from. Also remember array indexing [a[i]] and pointer arithmetic and de-referencing [*(a+i)] are synonymous expression. So in your second case, you end up updating the original array that was passed.

    Now there is a subtle difference between both the declarations. In case of f1 , the parameter b is a reference to the original array a, so b is still the same array with all the characteristic of the source array. In case of the second expression, b is just a pointer that incidentally points to the location of the array a.

    Considering the fact that the parameters are ideally different, they have different behavior

    1. Sizeof for the reference variable returns the actual size of the array.
    2. You cannot modify the reference parameter inside the function.

    For the sake of completeness, here is an updated program which will give you more idea on how the behavior differs.

    #include <iostream>
    using namespace std;
    
    void f1(int (&b)[5])
    {
        std::cout<<sizeof(b)<<std::endl;
        //b = new int[5]; -- Will give compile error
        b[0]=1;
    }
    
    void f2(int b[5])
    {
        std::cout<<sizeof(b)<<std::endl;
        //b = new int[5]; -- Will compile fine
        b[1]=2;
    }
    int main()
    {
        int a[5];
        f1(a);
        f2(a);
    
        cout<<a[0]<<endl;
        cout<<a[1]<<endl;
    }
    

    这篇关于C ++ - 通过引用传递数组,奇怪的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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