The Pointer as a Variable or a Constant


A pointer may be a variable or a constant. Lets examine both possibilities.

Pointer as a Variable

The preceding program had the pointer pointing to one integer variable. However, a pointer variable, being a variable, can point to different variables at different times in the program. In the following program, the value of the pointer is changed to point to two different integer variables .

 #include <iostream> using namespace std; int main () {  int num1 = 5, num2 = 14;  int* iPtr = &num1;  cout << "The value of num1 is " << num1 << endl;  *iPtr *= 2;  cout << "The value of num1 after *iPtr *= 2 is "   << *iPtr << endl;  iPtr = &num2;  cout << "The value of num2 is " << num2 << endl;  *iPtr /= 2;  cout << "The value of num after *iPtr /= 2 is "   << *iPtr << endl;  return 0; } 

The resulting output is therefore:

 The value of num1 is 5 The value of num1 after *iPtr *= 2 is 10 The value of num2 is 14 The value of num after *iPtr /= 2 is 7 

The Array Name as a Constant Pointer

While the pointer may be a variable, it also may be a constant. Indeed, in the previous chapter we actually discussed a constant pointer: the name of an array.

As you may recall from Chapter 10, the value of the name of an array is the base address of the array, which also is the address of the first element of an array. Thus, in the following program, both testScore and & testScore[0] have the same value.

 #include <iostream> using namespace std; const int MAX = 3; int main () {  int testScore[MAX] = {4, 7, 1};  cout << "The address of the array using testScore is "         << testScore << endl;  cout << "The address of the first element of the array "  "using &testScore[0] is " << &testScore[0] << endl;  cout << "The value of the first element of the array "  "using *testScore is " << *testScore << endl;   cout << "The value of the first element of the array "  "using testScore[0] is " << testScore[0] << endl;  return 0; } 

The resulting output is

 The address of the array using testScore is 0012FECC The address of the first element of the array using &testScore[0] is 0012FECC The value of the first element of the array using *testScore is 4 The value of the first element of the array using testScore[0] is 4 

Similarly, if you dereference the name of an array, its value is the same as the value of the first element of the array. Therefore, in the preceding program, both *testScore and testScore[0] have the same value.

However, you cannot change the value of the name of the array. For example, a statement such as testScore++ would result in a compiler error, the error message being ++ needs l-value. As you may recall from Chapter 10, the term l-value refers to the value to the left of the assignment operator. This error message is another way of saying you cant increment a constant because that would be changing the value of a constant after you declare it.




C++ Demystified(c) A Self-Teaching Guide
C++ Demystified(c) A Self-Teaching Guide
ISBN: 72253703
EAN: N/A
Year: 2006
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net