Home arrow Practices arrow Page 3 - What`s the Address? Pointers

The Array Name as a Constant Pointer - Practices

The name "pointer" describes the job of the item; a pointer "points" to another variable or constant. Some tasks in C++ are easier to do with pointers; others would be utterly impossible without pointers. This article discusses how to create and work with pointers in C++. It is taken from chapter 11 of the book C++ Demystified, written by Jeff Kent (McGraw-Hill/Osborne, 2004; ISBN: 0072253703).

TABLE OF CONTENTS:
  1. What`s the Address? Pointers
  2. Assigning a Pointer the Address of a Variable or Constant
  3. The Array Name as a Constant Pointer
  4. Incrementing a Pointer
  5. Passing an Array Using Pointer Notation
  6. Dynamic Memory Allocation
  7. Returning Pointers from Function s
  8. Summary
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 16
September 01, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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 canít increment a constant because that would be changing the value of a constant after you declare it.

Pointer Arithmetic

The value of a pointer, even though it is an address, is a numeric value. Therefore, you can perform arithmetic operations on a pointer just as you can a numeric value.

Using a Variable Pointer to Point to an Array

Pointer arithmetic is done often with arrays. However, since you cannot change the value of the name of an array, it being a constant pointer, you first should declare a variable pointer and then assign it to the address of an array.

So, we begin with an established point of reference, letís start with the following program, which outputs the address and value at each element of an array using the name of the array:

#include <iostream>> using namespace std; const int MAX = 3;

int main ()
{ int testScore[MAX] = {4, 7, 1};
  for (int i = 0; i < MAX; i++)
  {
    cout << "The address of index " << i << " of the array is "<< &testScore[i] << endl;
    cout << "The value at index " << i
<< " of the array is "<< testScore[i] << endl;
  }
  return 0;
}

The resulting output is

The address of index 0 of the array is 0012FECC
The value at index 0 of the array is 4
The address of index 1 of the array is 0012FED0
The value at index 1 of the array is 7
The address of index 2 of the array is 0012FED4
The value at index 2 of the array is 1

This program used the name of the array, testScore, to access, by index, each element of the array. The name of the array is a constant pointer. The following program modifies the previous program by using a variable pointer, iPtr, to access by index each element of the array.

#include <iostream>> using namespace std; const int MAX = 3;

int main ()
{
  int testScore[MAX] = {4, 7, 1};
  int* iPtr = testScore;
  for (int i = 0; i < MAX; i++)
  {
    cout << "The address of index " << i << " of the array is "<< & iPtr[i] << endl;
    cout << "The value at index " << i << " of the array is "<< iPtr[i] << endl;
 
}
 
return 0; 
}

The following statement in this program sets the variable pointer iPtr to point to the same address as the array name testScore:

int* iPtr = testScore;

The array name is not preceded with the address operator (&) because the array name already is an address, namely, the base address of the array. Therefore, after this assignment, iPtr and testScore both point to the beginning of the array. Accordingly, as shown in Figure 11-2, iPtr[2] and testScore[2] have the same value.



 
 
>>> More Practices Articles          >>> More By McGraw-Hill/Osborne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PRACTICES ARTICLES

- Calculating Development Project Costs
- More Techniques for Finding Things
- Finding Things
- Finishing the System`s Outlines
- The System in So Many Words
- Basic Data Types and Calculations
- What`s the Address? Pointers
- Design with ArgoUML
- Pragmatic Guidelines: Diagrams That Work
- Five-Step UML: OOAD for Short Attention Span...
- Five-Step UML: OOAD for Short Attention Span...
- Introducing UML: Object-Oriented Analysis an...
- Class and Object Diagrams
- Class Relationships
- Classes

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: