Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

4.2.4A Pointer to a Pointer

Instead of pointing to a regular variable, a pointer can be made to point to another pointer. To apply this, always remember that you must specify what target a pointer is pointing to. Consider the following example:

using namespace System;

Int main()

{

int value = 26;

Int *pointer;

pointer = &value;

Console::WriteLine(" Value = {0}", value);

Console::WriteLine("*Pointer = {0}", *pointer);

return 0;

}

This would produce:

Value = 26

*Pointer = 26

In this program, if necessary, you can declare a new variable that is a pointer that itself points to another pointer. When declaring such a variable, precede it with two *. After declaring the pointer, before using it, you must initialize it with a reference to a pointer, that is, a reference to a variable that was declared as a pointer. Here is an example:

using namespace System;

Int main()

{

int value = 26;

Int *pointer;

Int **pointerToPointer;

pointer = &value;

pointerToPointer = &pointer;

Console::WriteLine(" Value = {0}", value);

Console::WriteLine(" *Pointer = {0}", *pointer);

Console::WriteLine("**Pointer = {0}", **pointerToPointer);

return 0;

}

This would produce:

Value = 26

*Pointer = 26

**Pointer = 26

Just as demonstrated earlier, after initializing a pointer, if you change the value of the variable it points to, the pointer would be updated. Consider the following program:

using namespace System;

Int main()

{

int value = 26;

Int *pointer;

Int **pointerToPointer;

pointer = &value;

pointerToPointer = &pointer;

Console::WriteLine(" Value = {0}", value);

Console::WriteLine(" *Pointer = {0}", *pointer);

Console::WriteLine("**Pointer = {0}\n", **pointerToPointer);

value = 4805;

Console::WriteLine("After changing the value of the main variable...");

Console::WriteLine(" Value = {0}", value);

Console::WriteLine(" *Pointer = {0}", *pointer);

Console::WriteLine("**Pointer = {0}\n", **pointerToPointer);

return 0;

}

This would produce:

Value = 26

*Pointer = 26

**Pointer = 26

After changing the value of the main variable...

Value = 4805

*Pointer = 4805

**Pointer = 4805

Notice that, by changing the value of the original variable, when accessing its pointer or the pointer to its pointer, they reflect the new value. In the same way, instead of (directly) changing the value of the variable, you can change the value of its pointer. You can also change the value of the pointer to its pointer: all variables would be updated. Consider the following program:

using namespace System;

Int main()

{

int value = 26;

Int *pointer;

Int **pointerToPointer;

pointer = &value;

pointerToPointer = &pointer;

Console::WriteLine(" Value = {0}", value);

Console::WriteLine(" *Pointer = {0}", *pointer);

Console::WriteLine("**Pointer = {0}\n", **pointerToPointer);

value = 4805;

Console::WriteLine("After changing the value of the main variable...");

Console::WriteLine(" Value = {0}", value);

Console::WriteLine(" *Pointer = {0}", *pointer);

Console::WriteLine("**Pointer = {0}\n", **pointerToPointer);

*pointer = -728;

Console::WriteLine("After changing the value of the main variable...");

Console::WriteLine(" Value = {0}", value);

Console::WriteLine(" *Pointer = {0}", *pointer);

Console::WriteLine("**Pointer = {0}\n", **pointerToPointer);

**pointerToPointer = 945580;

Console::WriteLine("After changing the value of the main variable...");

Console::WriteLine(" Value = {0}", value);

Console::WriteLine(" *Pointer = {0}", *pointer);

Console::WriteLine("**Pointer = {0}\n", **pointerToPointer);

return 0;

}

This would produce:

Value = 26

*Pointer = 26

**Pointer = 26

After changing the value of the main variable...

Value = 4805

*Pointer = 4805

**Pointer = 4805

After changing the value of the main variable...

Value = -728

*Pointer = -728

**Pointer = -728

After changing the value of the main variable...

Value = 945580

*Pointer = 945580

**Pointer = 945580