问题理解按值传递并通过引用传递数组以及内存中发生的事情 [英] Problem understanding pass by value and pass by reference of arrays and what happens in the memory

查看:73
本文介绍了问题理解按值传递并通过引用传递数组以及内存中发生的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题理解传递值并通过引用传递和

想知道它们是如何或出现在记忆中的:


我必须让我的基础知识再次正确。我创建一个数组并尝试所有传递数组的方法。在下面的代码中,fun1(int

a1 []) - 与fun1(int * a1)相同 - 其中两者都是由

引用传递的类型。在这个函数中,创建了另一个指针a1,它的

地址& a1与传递的数组a的不同,在main方法中是& a

。但是,

调用者或主函数中[0]和[1]的地址与数组a1 [0]和a1 [1]
$ b的地址相同调用函数fun1中的$ b(int a1 [])。此函数中对数组

的任何修改都将反映在main方法中。如何以及在哪里

这些变量是在堆栈,堆,表中创建的,以及

之后和调用函数之前会发生什么?


让我们看看另一个函数fun2(int *& a2)我试图再次通过引用传递
,但这次我不想创建另一个

指针但直接处理指向

数组的原始指针。当我将此数组传递给此函数时,它返回

跟随错误,我不明白


fun2(a); //错误:

的非const引用的无效初始化//从类型为''int *''的临时类型中输入''int *&''

但是,如果我创建另一个指针int * aPtr = a;然后传递它

作为fun2(aPtr);它工作正常。同样在这种情况下,我在调用者(主)和被调用(fun2)方法中直接使用aPtr工作
。即,& aPtr

在调用者和被调用函数中是相同的,& a2在fun2(int *& a2)中,

与前一种情况不同,即使这样是通过引用传递。我没有

有时间搞清楚这个问题,所以我在这里发帖讨论

讨论并想了解详情,关于这些怎么样? >
变量在内存中创建或分配。

#include< iostream>

使用命名空间std;


void fun1(int a1 [])//与fun相同(int * p)

{

cout<< In fun(int a1 [])" << a1 << a1<< " & a1" << & a1<<

& a1 [0]" << & a1 [0]<< endl;

// int a1 []位于不同的位置(不同& a)但是

//正确地址为a1 [0]和a1 [ 1]

a1 [0] = 10;

a1 [1] = 20;

}


void fun2(int *& a2)

{

cout<< 在fun1(int *& a2)中 << " a2 << a2<< " & a2" << & a2<<

& a2 [0]" << & a2 [0]<<结束;

a2 [0] = 30;

a2 [1] = 50;

}


int main()

{

int a [2] = {0,1};

cout<< Main:a << a<< " & a << & a<< " & a [0]" << & a [0]<<

endl;


// fun2(a); //错误:无法初始化非const

引用

//从类型为''int *''的临时类型中输入''int *&''

fun1(a);

cout<< 乐趣之后(a): << a [0]<< ''''<< a [1]<< endl;


int * aPtr = a;

cout<< 传递其& aPtr的aPtr。 << & aPtr<< " aPtr << aPtr

<< endl;

fun2(aPtr); //这里没关系,你也可以用它作为fun1(aPtr)

cout<< 在fun1之后(aPtr): << a [0]<< ''''<< a [1]<< endl;


int cInt = 10;

// int *& cPtr =& cInt; //错:你正在尝试逻辑错误

创建一个类型为int *&的
//非const引用来自

a

//临时类型为int *

int * cPtr =& cInt; //将cInt的地址存储在cPtr中

int *& cRefPtr = cPtr; //参考cPtr


}


输出:


主要:a 0x22ff80& a 0x22ff80& a [0] 0x22ff80

乐趣(int a1 [])a1 0x22ff80& a1 0x22feb0& a1 [0] 0x22ff80

之后fun(a):10 20

传递aPtr,其中& aPtr 0x22ff7c aPtr 0x22ff80

在fun1(int *& a2)a2 0x22ff80& a2 0x22ff7c& a2 [0] 0x22ff80

之后fun1(aPtr):30 50

I have problem understanding pass by value and pass by reference and
want to how how they are or appear in the memory:

I had to get my basics right again. I create an array and try all
possible ways of passing an array. In the following code, fun1(int
a1[]) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed array a, which is &a
in the main method. However, the addresses of a[0] and a[1] in the
caller or main function are same as those of the array a1[0] and a1[1]
in the called function fun1(int a1[]). Any modifications to the array
in this function will reflect in the main method. How and where are
these variables created in stack, heap, tables and what happens after
and before calling the function?

Lets look at the other function fun2(int *&a2) where I am trying to
again pass by reference, but this time I do not want to create another
pointer but directly work on the original pointer pointing to the
array. When I pass this array a to this function it returns the
following error which I did not understand

fun2(a); // ERROR: invalid initialization of non-const reference of
//type ''int*&'' from a temporary of type ''int*''

however, if I create another pointer int *aPtr = a; and then pass it
as fun2(aPtr); it works fine. Also in this case, I am working directly
with aPtr in both caller(main) and called (fun2) methods. i.e., &aPtr
is same in the caller and called functions, &a2 in fun2(int* &a2),
unlike the previous case, even this is pass by reference. I did not
have time to figure out the problem, so I am posting it here for
discussion and would like to know the details, as to how these
variables are created or assigned in memory.
#include<iostream>
using namespace std;

void fun1(int a1[]) // same as fun(int* p)
{
cout << "In fun(int a1[]) " << "a1 " << a1 << " &a1 " << &a1 << "
&a1[0] " << &a1[0] << endl;
// int a1[] is in a different location (different &a) but has the
// correct addresses of a1[0] and a1[1]
a1[0] = 10;
a1[1] = 20;
}

void fun2(int* &a2)
{
cout << "In fun1(int* &a2) " << " a2 " << a2 << " &a2 " << &a2 << "
&a2[0] " << &a2[0] << endl;
a2[0] = 30;
a2[1] = 50;
}

int main()
{
int a[2] = {0,1};
cout << "Main: a " << a << " &a " << &a << " &a[0] " << &a[0] <<
endl;

//fun2(a); // ERROR: invalid initialization of non-const
reference of
//type ''int*&'' from a temporary of type ''int*''
fun1(a);
cout << "After fun(a): " << a[0] << '' '' << a[1] << endl;

int *aPtr = a;
cout << "Passing aPtr whose &aPtr " << &aPtr << " aPtr " << aPtr
<< endl;
fun2(aPtr); // Here it is ok, you can also use it as fun1(aPtr)
cout << "After fun1(aPtr): " << a[0] << '' '' << a[1] << endl;

int cInt = 10;
//int* &cPtr = &cInt; // WRONG:logically wrong as you are trying
to create a
// non-const reference of type int* & from
a
// temporary of type int*
int* cPtr = &cInt; // store the address of cInt in cPtr
int* &cRefPtr = cPtr; // make a reference of the cPtr

}

output:

Main: a 0x22ff80 &a 0x22ff80 &a[0] 0x22ff80
In fun(int a1[]) a1 0x22ff80 &a1 0x22feb0 &a1[0] 0x22ff80
After fun(a): 10 20
Passing aPtr whose &aPtr 0x22ff7c aPtr 0x22ff80
In fun1(int* &a2) a2 0x22ff80 &a2 0x22ff7c &a2[0] 0x22ff80
After fun1(aPtr): 30 50

推荐答案

" venkatagmail" < ch ******** @ gmail.com写信息

新闻:11 ********************** @ 22g2000hsm.googlegro ups.com ...
"venkatagmail" <ch********@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...

>我有问题理解按值传递并通过引用传递和

想要它们是如何存在或出现在记忆中的:


我必须再次掌握我的基础知识。我创建一个数组并尝试所有传递数组的方法。在下面的代码中,fun1(int

a1 []) - 与fun1(int * a1)相同 - 其中两者都是由

引用传递的类型。在这个函数中,创建了另一个指针a1,它的

地址& a1与传递的数组a的不同,在main方法中是& a

。但是,

调用者或主函数中[0]和[1]的地址与数组a1 [0]和a1 [1]
$ b的地址相同调用函数fun1中的$ b(int a1 [])。此函数中对数组

的任何修改都将反映在main方法中。如何以及在哪里

这些变量是在堆栈,堆,表中创建的,以及

之后和调用函数之前会发生什么?


让我们看看另一个函数fun2(int *& a2)我试图再次通过引用传递
,但这次我不想创建另一个

指针但直接处理指向

数组的原始指针。当我将此数组传递给此函数时,它返回

跟随错误,我不明白


fun2(a); //错误:

的非const引用的无效初始化//从类型为''int *''的临时类型中输入''int *&''

但是,如果我创建另一个指针int * aPtr = a;然后传递它

作为fun2(aPtr);它工作正常。同样在这种情况下,我在调用者(主)和被调用(fun2)方法中直接使用aPtr工作
。即,& aPtr

在调用者和被调用函数中是相同的,& a2在fun2(int *& a2)中,

与前一种情况不同,即使这样是通过引用传递。我没有

有时间搞清楚这个问题,所以我在这里发帖讨论

讨论并想了解详情,关于这些怎么样? >
变量在内存中创建或分配。


#include< iostream>

using namespace std;


void fun1(int a1 [])//和fun一样(int * p)

{
>I have problem understanding pass by value and pass by reference and
want to how how they are or appear in the memory:

I had to get my basics right again. I create an array and try all
possible ways of passing an array. In the following code, fun1(int
a1[]) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed array a, which is &a
in the main method. However, the addresses of a[0] and a[1] in the
caller or main function are same as those of the array a1[0] and a1[1]
in the called function fun1(int a1[]). Any modifications to the array
in this function will reflect in the main method. How and where are
these variables created in stack, heap, tables and what happens after
and before calling the function?

Lets look at the other function fun2(int *&a2) where I am trying to
again pass by reference, but this time I do not want to create another
pointer but directly work on the original pointer pointing to the
array. When I pass this array a to this function it returns the
following error which I did not understand

fun2(a); // ERROR: invalid initialization of non-const reference of
//type ''int*&'' from a temporary of type ''int*''

however, if I create another pointer int *aPtr = a; and then pass it
as fun2(aPtr); it works fine. Also in this case, I am working directly
with aPtr in both caller(main) and called (fun2) methods. i.e., &aPtr
is same in the caller and called functions, &a2 in fun2(int* &a2),
unlike the previous case, even this is pass by reference. I did not
have time to figure out the problem, so I am posting it here for
discussion and would like to know the details, as to how these
variables are created or assigned in memory.
#include<iostream>
using namespace std;

void fun1(int a1[]) // same as fun(int* p)
{



a1是a局部变量,其内容将是传入的指针。

a1 is a local variable who''s contents will be a pointer that is passed in.


cout<< In fun(int a1 [])" << a1 << a1<< " & a1" << & a1<<

& a1 [0]" << & a1 [0]<< ENDL;
cout << "In fun(int a1[]) " << "a1 " << a1 << " &a1 " << &a1 << "
&a1[0] " << &a1[0] << endl;



输出a1将输出它的内容,这是指向传递的指针

in,它指向在main中声明的数组。

输出& a1将输出在堆栈/堆上存储的本地指针的地址



输出& a1 [ 0]将输出a1指向

的第一个int的地址,这与a1本身相同。


假设一个4字节的int。调用此函数时,设置4个字节

除了保持参数a1(或sizeof int *)。那些4个字节可以在堆内存的任何地方都是
(我相信编译器可以在任何地方免费使用

,堆最常见。如果他们可能错了可以随意使用

。我没有标准副本。在你的情况下它是内存

locatoin 0x22feb0到0x22feb3。当调用此函数并传递

指针被推入堆栈的指针的参数。调用

函数。弹出该值并将其复制到局部变量a1(0x22feb0到0x22feb3)的位置。你的情况下这4个字节

由指针值0x22ff80组成。


现在,如果你看一下变量a1的地址,你将得到0x22f3b3。

如果看一下它的内容,你会得到0x22ff80。如果你取消引用它

(看看指针指向的位置)你将获得

数组中的值。这就是为什么& a1 [0]返回原始数组的地址。

a1 [0]表示查看指针所指向的第0个元素。 &

表示获取它的地址。


编译器可以自由地做一些不同的事情,我不知道他们是什么<允许并允许不允许使用
,但传递指针通常是以这种方式处理的。

Outputting a1 will output the contents of it, which is the pointer passed
in, which points to the array a declared in main.
Ouputting &a1 will output the address of the local pointer which is stored
on the stack/heap.
Outputting &a1[0] will output the address of the first int that a1 points
to, which is the same as a1 itself.

Lets presume a 4 byte int. When this function is called 4 bytes are set
aside to hold the paramater a1 (or sizeof int*). Those 4 bytes can be
anywhere in memory where the heap is (I believe compilers are free to use
anywhere, heap is most common. I may be wrong on if they are free to use
wherever or not. I don''t have copy of standard). In your case it is memory
locatoin 0x22feb0 to 0x22feb3. When this function is called and you pass
the parameter of the pointer that pointer is pushed onto the stack. The
function is called. The value is popped off and copied into the location of
the local variable a1 (0x22feb0 to 0x22feb3). These 4 bytes in your case
consist of the pointer value 0x22ff80.

Now, if you look at the address of the variable a1 you will get 0x22f3b3.
If you look at it''s contents you will get 0x22ff80. If you dereference it
(look at where the pointer is pointing to) you''ll get the values in the
array. Which is why &a1[0] returns the address of the original array.
a1[0] says look at the 0th element of where the pointer is pointing to. &
means get the address of it.

Compilers are free to do somethings diffrent, I''m not aware of what they''re
allowed and not allowed to do different, but passing pointers is generally
handled this way.


// int a1 []位于不同的位置(不同& a),但是

//正确地址为a1 [0]和a1 [1]

a1 [ 0] = 10;

a1 [1] = 20;

}


void fun2(int *& a2)

{

cout<< 在fun1(int *& a2)中 << " a2 << a2<< " & a2" << & a2<<

& a2 [0]" << & a2 [0]<<结束;

a2 [0] = 30;

a2 [1] = 50;

}


int main()

{

int a [2] = {0,1};

cout<< Main:a << a<< " & a << & a<< " & a [0]" << & a [0]<<

endl;


// fun2(a); //错误:无法初始化非const

引用

//从类型为''int *''的临时类型中输入''int *&''

fun1(a);

cout<< 乐趣之后(a): << a [0]<< ''''<< a [1]<< endl;


int * aPtr = a;

cout<< 传递其& aPtr的aPtr。 << & aPtr<< " aPtr << aPtr

<< endl;

fun2(aPtr); //这里没关系,你也可以用它作为fun1(aPtr)

cout<< 在fun1之后(aPtr): << a [0]<< ''''<< a [1]<< endl;


int cInt = 10;

// int *& cPtr =& cInt; //错:你正在尝试逻辑错误

创建一个类型为int *&的
//非const引用来自

a

//临时类型为int *

int * cPtr =& cInt; //将cInt的地址存储在cPtr中

int *& cRefPtr = cPtr; //参考cPtr


}


输出:


主要:a 0x22ff80& a 0x22ff80& a [0] 0x22ff80

乐趣(int a1 [])a1 0x22ff80& a1 0x22feb0& a1 [0] 0x22ff80

之后fun(a):10 20

传递aPtr,其中& aPtr 0x22ff7c aPtr 0x22ff80

在fun1(int *& a2)a2 0x22ff80& a2 0x22ff7c& a2 [0] 0x22ff80

之后fun1(aPtr):30 50
// int a1[] is in a different location (different &a) but has the
// correct addresses of a1[0] and a1[1]
a1[0] = 10;
a1[1] = 20;
}

void fun2(int* &a2)
{
cout << "In fun1(int* &a2) " << " a2 " << a2 << " &a2 " << &a2 << "
&a2[0] " << &a2[0] << endl;
a2[0] = 30;
a2[1] = 50;
}

int main()
{
int a[2] = {0,1};
cout << "Main: a " << a << " &a " << &a << " &a[0] " << &a[0] <<
endl;

//fun2(a); // ERROR: invalid initialization of non-const
reference of
//type ''int*&'' from a temporary of type ''int*''
fun1(a);
cout << "After fun(a): " << a[0] << '' '' << a[1] << endl;

int *aPtr = a;
cout << "Passing aPtr whose &aPtr " << &aPtr << " aPtr " << aPtr
<< endl;
fun2(aPtr); // Here it is ok, you can also use it as fun1(aPtr)
cout << "After fun1(aPtr): " << a[0] << '' '' << a[1] << endl;

int cInt = 10;
//int* &cPtr = &cInt; // WRONG:logically wrong as you are trying
to create a
// non-const reference of type int* & from
a
// temporary of type int*
int* cPtr = &cInt; // store the address of cInt in cPtr
int* &cRefPtr = cPtr; // make a reference of the cPtr

}

output:

Main: a 0x22ff80 &a 0x22ff80 &a[0] 0x22ff80
In fun(int a1[]) a1 0x22ff80 &a1 0x22feb0 &a1[0] 0x22ff80
After fun(a): 10 20
Passing aPtr whose &aPtr 0x22ff7c aPtr 0x22ff80
In fun1(int* &a2) a2 0x22ff80 &a2 0x22ff7c &a2[0] 0x22ff80
After fun1(aPtr): 30 50



谢谢,就我而言从你的理解中,当我走的时候纠正我

错了。


当我开始在main()中声明一个数组''a''时,他们在堆中创建了
,一个[0],一个[1]指向我的第一个元素和第二个元素a b $ b。 a的值包含

a [0]的地址,我使用它作为指向int的指针传递给fun1(int

a1 [])。在我调用此函数之前,我的所有自动变量都将

移动到堆栈中,并且调用函数的地址存储在堆栈的顶部

中以返回到操作系统我相信?


一旦我进入被调用的方法fun1(int a1 []),我就可以访问数组的第一个位置

了a [0](已经将
推入堆栈?)现在存储在a1(a1位于
堆中的不同位置),所以一旦我做了修改到a1 [0],我是否在堆栈中对原始值进行
修改? (我知道不能直接修改
)或者仅在我退出

函数fun1之后才进行修改,但这可能是a1 [0],a1 [1 ]和[0],

a [1]指向相同的地址位置。我很困惑如何在传递值的情况下发生内存

,使用

指针传递引用并使用引用。这只是理解内存分配的问题的开始。我上面发布的原始问题还有很多要问的问题。

Thank you, as far as I understand from yours, correct me when I go
wrong.

when I start off with my declaration of an array ''a'' in main(), they
are created in the heap?, with a[0] ,a[1] pointing to my first and
second elements of array a. The value of a contains the address of
a[0] using which I use to pass as a pointer to an int to fun1(int
a1[]). Before I call this function, all of my automatic variables move
into stack and the address of the caller function is stored at the top
of the stack to be returned to the operating system I believe?

Once I enter the called method fun1(int a1[]) I have access to the
first location of the array which is a[0] (which is already pushed
into stack??) now stored in a1 (a1 is in a different location in
heap), so once I make modifications to a1[0], am I making
modifications to the original value in stack?? (which I know cannot be
modified directly) or are these modified only after I exit the
function fun1, which however is doubtful as a1[0], a1[1] and a[0],
a[1] point to same address locations. I am confused how the memory
thing takes place in case of pass by value, pass by reference using
pointer and using a reference. This is just the beginning of my
problem in understanding memory assignments. The original problem I
posted above has lot more to ask.


venkatagmail写道:
venkatagmail wrote:

我有问题理解传递值并通过引用传递

想知道它们是如何存在或出现在内存中的:


我必须再次掌握我的基础知识。我创建一个数组并尝试所有传递数组的方法。在下面的代码中,fun1(int

a1 []) - 与fun1(int * a1)相同 - 其中两者都是由

引用传递的类型。在这个函数中,创建了另一个指针a1,它的

地址& a1与传递的数组a的不同,在main方法中是& a

。但是,

调用者或主函数中[0]和[1]的地址与数组a1 [0]和a1 [1]
$ b的地址相同调用函数fun1中的$ b(int a1 [])。此函数中对数组

的任何修改都将反映在main方法中。如何以及在哪里

这些变量是在堆栈,堆,表中创建的,以及

之后和调用函数之前会发生什么?


让我们看看另一个函数fun2(int *& a2)我试图再次通过引用传递
,但这次我不想创建另一个

指针但直接处理指向

数组的原始指针。当我将此数组传递给此函数时,它返回

跟随错误,我不明白


fun2(a); //错误:

的非const引用的无效初始化//从类型为''int *'的临时类型''int *&''
I have problem understanding pass by value and pass by reference and
want to how how they are or appear in the memory:

I had to get my basics right again. I create an array and try all
possible ways of passing an array. In the following code, fun1(int
a1[]) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed array a, which is &a
in the main method. However, the addresses of a[0] and a[1] in the
caller or main function are same as those of the array a1[0] and a1[1]
in the called function fun1(int a1[]). Any modifications to the array
in this function will reflect in the main method. How and where are
these variables created in stack, heap, tables and what happens after
and before calling the function?

Lets look at the other function fun2(int *&a2) where I am trying to
again pass by reference, but this time I do not want to create another
pointer but directly work on the original pointer pointing to the
array. When I pass this array a to this function it returns the
following error which I did not understand

fun2(a); // ERROR: invalid initialization of non-const reference of
//type ''int*&'' from a temporary of type ''int*''



a是数组类型,绑定到固定地址。所以你不能将b $ b改为指向另一个。当你为一个函数传递一个数组作为参数

时,这里有一个衰减类型为/ int * const /,所以你可以将

转换为/ int *& /。

a is of array type, which is bound to a fixed address. so you can''t
change a to point to another one. when you pass an array as a parameter
for a function, here a decays to type /int* const/, so you can convert
it to /int*&/.


>

但是,如果我创建另一个指针int * aPtr = a;然后传递它
>
however, if I create another pointer int *aPtr = a; and then pass it



转换自/ int * const /到/ int * /是允许的,/ aPtr /指向

其中/ a / points,但/ aPtr /无法修改/ a /指向的位置。

这就像:


const int S = 100;

int i = S;

conversion from /int* const/ to /int*/ is allowed, /aPtr/ points to
where /a/ points, but /aPtr/ has no way modify where /a/ points to.
this is just like:

const int S = 100;
int i = S;


as fun2(aPtr);它工作正常。同样在这种情况下,我在调用者(主)和被调用(fun2)方法中直接使用aPtr工作
。即,& aPtr

在调用者和被调用函数中是相同的,& a2在fun2(int *& a2)中,

与前一种情况不同,即使这样是通过引用传递。我没有

有时间搞清楚这个问题,所以我在这里发帖讨论

讨论并想了解详情,关于这些怎么样? >
变量在内存中创建或分配。
as fun2(aPtr); it works fine. Also in this case, I am working directly
with aPtr in both caller(main) and called (fun2) methods. i.e., &aPtr
is same in the caller and called functions, &a2 in fun2(int* &a2),
unlike the previous case, even this is pass by reference. I did not
have time to figure out the problem, so I am posting it here for
discussion and would like to know the details, as to how these
variables are created or assigned in memory.



要理解这些,你已经学会了如何找到constness所在的位置。


as / aPtr /是typeof' 'int *''不是''int * const'',那么你可以将/ aPtr /

指向''int''类型的任何地方,并且因为不是''const int' ',因为我们

可以修改/ aPtr /指向的内容。

To understand these, you have learn how to find where the constness lays.

as /aPtr/ is typeof ''int*'' not ''int* const'', then you can have /aPtr/
point to anywhere of ''int'' type, and of cause NOT to ''const int'', as we
can modify the content where /aPtr/ points to.


>


#include< iostream>

使用命名空间std;


void fun1(int a1 [])//与fun相同(int * p)

{

cout<< In fun(int a1 [])" << a1 << a1<< " & a1" << & a1<<

& a1 [0]" << & a1 [0]<< endl;

// int a1 []位于不同的位置(不同& a)但是

//正确地址为a1 [0]和a1 [ 1]

a1 [0] = 10;

a1 [1] = 20;

}


void fun2(int *& a2)

{

cout<< 在fun1(int *& a2)中 << " a2 << a2<< " & a2" << & a2<<

& a2 [0]" << & a2 [0]<<结束;

a2 [0] = 30;

a2 [1] = 50;

}


int main()

{

int a [2] = {0,1};

cout<< Main:a << a<< " & a << & a<< " & a [0]" << & a [0]<<

endl;


// fun2(a); //错误:无法初始化非const

引用

//从类型为''int *''的临时类型中输入''int *&''

fun1(a);

cout<< 乐趣之后(a): << a [0]<< ''''<< a [1]<< endl;


int * aPtr = a;

cout<< 传递其& aPtr的aPtr。 << & aPtr<< " aPtr << aPtr

<< endl;

fun2(aPtr); //这里没关系,你也可以用它作为fun1(aPtr)

cout<< 在fun1之后(aPtr): << a [0]<< ''''<< a [1]<< endl;


int cInt = 10;

// int *& cPtr =& cInt; //错:你正在尝试逻辑错误

创建一个类型为int *&的
//非const引用来自

a

//临时类型为int *

int * cPtr =& cInt; //将cInt的地址存储在cPtr中

int *& cRefPtr = cPtr; //参考cPtr


}


输出:


主要:a 0x22ff80& a 0x22ff80& a [0] 0x22ff80

乐趣(int a1 [])a1 0x22ff80& a1 0x22feb0& a1 [0] 0x22ff80

之后fun(a):10 20

传递aPtr,其中& aPtr 0x22ff7c aPtr 0x22ff80

在fun1(int *& a2)a2 0x22ff80& a2 0x22ff7c& a2 [0] 0x22ff80

之后fun1(aPtr):30 50
>

#include<iostream>
using namespace std;

void fun1(int a1[]) // same as fun(int* p)
{
cout << "In fun(int a1[]) " << "a1 " << a1 << " &a1 " << &a1 << "
&a1[0] " << &a1[0] << endl;
// int a1[] is in a different location (different &a) but has the
// correct addresses of a1[0] and a1[1]
a1[0] = 10;
a1[1] = 20;
}

void fun2(int* &a2)
{
cout << "In fun1(int* &a2) " << " a2 " << a2 << " &a2 " << &a2 << "
&a2[0] " << &a2[0] << endl;
a2[0] = 30;
a2[1] = 50;
}

int main()
{
int a[2] = {0,1};
cout << "Main: a " << a << " &a " << &a << " &a[0] " << &a[0] <<
endl;

//fun2(a); // ERROR: invalid initialization of non-const
reference of
//type ''int*&'' from a temporary of type ''int*''
fun1(a);
cout << "After fun(a): " << a[0] << '' '' << a[1] << endl;

int *aPtr = a;
cout << "Passing aPtr whose &aPtr " << &aPtr << " aPtr " << aPtr
<< endl;
fun2(aPtr); // Here it is ok, you can also use it as fun1(aPtr)
cout << "After fun1(aPtr): " << a[0] << '' '' << a[1] << endl;

int cInt = 10;
//int* &cPtr = &cInt; // WRONG:logically wrong as you are trying
to create a
// non-const reference of type int* & from
a
// temporary of type int*
int* cPtr = &cInt; // store the address of cInt in cPtr
int* &cRefPtr = cPtr; // make a reference of the cPtr

}

output:

Main: a 0x22ff80 &a 0x22ff80 &a[0] 0x22ff80
In fun(int a1[]) a1 0x22ff80 &a1 0x22feb0 &a1[0] 0x22ff80
After fun(a): 10 20
Passing aPtr whose &aPtr 0x22ff7c aPtr 0x22ff80
In fun1(int* &a2) a2 0x22ff80 &a2 0x22ff7c &a2[0] 0x22ff80
After fun1(aPtr): 30 50


这篇关于问题理解按值传递并通过引用传递数组以及内存中发生的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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