从指针数组的元素传递地址以设置另一个指针地址

Passing the address from an element of an array of pointers to set another pointer address

本文关键字:地址 指针 另一个 设置 数组 元素      更新时间:2023-10-16

我尝试过搜索响应,但不幸的是,示例似乎太复杂,我无法理解。最重要的是,我想知道如何传递和更改指针数组的地址。

示例:

class Foo {
M** arryM;
...
arryM = new M*[10];
...
void Foo::replace(int ind, M &newm) {
// Q1: which one correct to change the address of an element to a new object's address?
arryM[ind] = newm; // this correct to pass and reset a new address for pointer?
arryM[ind] = &newm; // or is this correct, or does this just set the object pointed by array element instead of the array element's address?
*arryM[ind] = newm; // or do I need to dereference the pointer rep by array element first?
&arryM[ind] = &newm; // or is this correct??
}
...
// Q2: is it easier to return a pointer or address for this function?
M & Foo::getM(int ind) {
M *out;
// Q3: which one of following correct to get correct address value to return?
out = arryM[ind]; // get address to M obj pointed by pointer array elem
out = *arryM[ind]; // is "arryM[ind]" == "*ptr"?? if so how to get to "ptr"?
out = &arryM[ind]; // is this way to get to "ptr" so addresses passed?
return out;
}
}
void main() {
M *op1;
M *op2;
M *sol;
Foo mstorage; // and assume already got valid M objects pointed to by arryM[] elements
...
// Q4: which one correct?
op1 = mstorage.getM(x); // x being the correct and valid int index pos
op1 = *mstorage.getM(x); // so address and not object pointed to is passed?
*op1 = mstorage.getM(x); 
op2 = mstorage.getM(y);
... // perform some operations using op1 and op2 M functions
int size = op1->getDim() + op2->getDim();
sol = new M(size, name); // no default constructor--M objects need a size parameter
...// perform/set values of sol based upon op1, op2 values
// Q5: this is correct to pass the address for sol object created, right?
if (<sol already exists>) mstorage.replace(indx, *sol);
else mstorage.add(*sol);
}

因此,我从代码示例中提取的5个主要问题如下,Q1、Q3和Q4对我来说最重要/最令人困惑:

Q1(重要):如果一个array[i]返回一个指针*n,那么我们如何将"array[i]"解引用为"n",这样我们就可以得到"n=p",其中"*p"有一个外部创建对象的地址。此外,其中"*p"通过类型为&参数

Q2:在给定的上下文中,在函数中返回指针或地址更好吗?或者,将上下文更改为使用一种函数返回类型比使用另一种更容易吗?

Q3(重要):如果我们返回一个地址,那么让"array[i]"(基于与Q1中相同的假设)成为"n"中包含的地址而不是"*n"表示的对象的正确方法是什么?

Q4(重要):如果函数正在返回一个地址&,那么本地创建的指针*p是否能够通过"p=obj.funcretaddress()"接收地址?我看到了一些带有doubles的示例代码,但这让我感到困惑:"doublehours=funcreturningaddress()"。

Q5:只需仔细检查一下,如果函数参数取"obj&x",我们本地创建一个"obj*y",在函数调用中我们会传递"function(*y)",然后在函数范围内传递"obj*n=&x",这样数组[i]表示的指针就可以取本地创建的obj的正确地址。

非常感谢,因为我已经编译和调整了好几天了,我在尝试从网络上获取int和double数据类型的示例并将它们应用于创建的类对象时感到困惑。

我很抱歉,如果有什么不清楚的地方,请告诉我!

编辑:非常感谢,我只是想澄清第四季度:

int a = 2;
int *b;
b = &a;
*b = 3;
int c = 4;
b = func(c); // *b now points to c, and value is 5?
*b = func(c); // *b value is updated to 5, and a therefore also now 5??
...
int & func(int &d) {
d++;
return &d; // correct, or wrong as &&d is returned?
return d; // and the & is applied to the var d?
}

Q1。以下两个是正确的。arrayM[ind]将返回指针。因此,假设您正在存储到指针中。要小心,关于您正在添加的对象(即newM)的生存期

arryM[ind] = &newm; // or is this correct, or does this just set the object pointed by array element instead of the array element's address?
*arryM[ind] = newm;

Q2.以下内容正确。

out = arryM[ind];

此外,当指针需要引用时,不能返回指针。所以,您可以将函数修改为发送方指针,也可以发送值而不是指针。

Q3.如果您返回的地址如下

return array[ind]

那么它将返回一个指针。(即M*myArray)。您可以访问以下元素

1. myArray[index]
2. *(myArray+1)

Q4.如果返回引用(即M&),则必须在引用或普通对象中收集它们,否则也可以将值存储到指针中,如下所示。

Function: M& foo()
Call: M m = foo()
OR M& m = foo()
OR M* m;  *m = foo();

问题5.我无法回答你的问题。我稍后会更新它。

如果你需要详细解释,请告诉我。