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

4.2.3Pointer Initialization

If you declare a value variable, you can initialize it with an appropriate value of its type. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

int Number = 224;

cout << "Address of Number: " << &Number;

Console::WriteLine();

return 0;

}

If you declare a pointer, because it doesn't hold an explicit value like a value variable, you cannot just initialize it with any value: you would receive an error. If you want, you initialize it with 0, considered the null value. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

int Nbr = 224;

int *Number = 0;

Console::WriteLine();

return 0;

}

Because a pointer "points" to another variable, to initialize it, you can assign it a declared and initialized variable preceded with the ampersand operator &. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

int Nbr = 224;

int *Number = &Nbr;

Console::WriteLine();

return 0;

}

You can also initialize a pointer on a different line, after declaring it. This time, you should not use the asterisk on the pointer, but the compiler should know that the pointer will point to a variable's address. Therefore, the name of the variable will still use the & operator. This is done as follows:

using namespace System;

Int main()

{

int Nbr = 224;

Int *Number;

Number = &Nbr;

Console::WriteLine();

return 0;

}

Just like a regular variable has an address, a pointer, as a variable, has an address too. To find out the address of a pointer variable, type the ampersand on its left, without the *.

Once you have declared and initialized a pointer variable, it holds the same value as the variable it points to. To display the value that a pointer holds, use its name preceded by the *. Here is an example:

using namespace System;

Int main()

{

int Nbr = 224;

Int *Number;

Number = &Nbr;

Console::Write("Number1: ");

Console::WriteLine(Nbr);

Console::Write("Number2: ");

Console::WriteLine(*Number);

return 0;

}

The program would produce:

Number1: 224

Number2: 224

If you want to display the value held by a pointer, make sure you know what variable the pointer is pointing to. In other words, make sure you initialize it. If you attempt to display the value of an un-initialized pointer, the compiler would throw an error and would stop. Therefore, before accessing the value of a pointer, know what value that pointer is holding. Otherwise, wait until you have initialized it before accessing its value:

You do not have to always initialize either the pointer or the variable it is pointing to. You can first declare both the variable and a pointer, then initialize them later on, when needed.

Once you have declared a variable and assign it to a pointer, during the course of your program, the value of a variable is likely to change but as long as the pointer points to the same variable, it would hold the same value as the variable it points to.