<分区>
考虑到这两个示例都发生在函数内部,*p_address= new int(2)
和通过 & p_address = &value
的赋值有什么区别?
例如:
我有 int 指针 *original_pointer。我将它的地址传递给函数。在函数内部,我创建了一个指向 2 的 int 值的 int 指针。然后我将指针(在函数内部创建)分配给 *original_pointer。当我在函数外cout *original_pointer 时,它返回-858993460,而在函数内它返回值 2。
但是,当我在函数内部使用new 创建指针时,函数内外的*original_pointer 的值是相同的。
这是代码:
int main() {
while (true) {
void assign_(const int**);
char* tmp = " ";
int const *original_pointer;
assign_(&original_pointer);
cout << "the address of original_pointer is " << original_pointer << endl;
cout << "the value of original_pointer is " << *original_pointer << endl;
cin >> tmp;
}
return 0;
}
void assign_( int const **addr) {
int* p_value;
int value = 2;
p_value = &value;
*addr = p_value;
//*addr = new RtFloat(2.0); // If I create the pointer this way the value of *addr is the same with *original_pointer
cout << "the adress of *addr inside the function is " << *addr << endl;
cout << "the value of **addr inside the function is " << **addr << endl;
}