- Forward


Pointers and References in C/C++
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation for Java Programmers
Back SMYC Forward
  • Some people say that Java is a "pointerless" language
  • In fact, Java uses references (which are related to pointers) but hides them from you
  • In C/C++ you don't have that luxury -- you have to worry about how and where variables are stored and how you can/should refer to them
Variables: Their Names, Values and Addresses
Back SMYC Forward
  • A Line of Code:
    • int i;
  • What Happens:
    • i is added to the name table
    • i is designated a value type
    • Memory is allocated for an int
    • The memory is not initialized
  • Where is the Memory?
    • Use &i
  • An Illustration:
    • static-memory1
Variables: Their Names, Values and Addresses (cont.)
Back SMYC Forward
  • Another Line of Code:
    • i = 5;
  • What Happens:
    • i is assigned the value 5
  • An Illustration:
    • static-memory2
Pointing to Memory
Back SMYC Forward
  • Declaring a Pointer:
    • Pointers are declared using *
  • An Example:
    • int *ptr;
  • Where is the Memory?
    • Use &ptr
    • (How much memory can be addressed in C++?)
  • An Illustration:
    • static-memory3
Pointing to Memory (cont.)
Back SMYC Forward
  • Using a Pointer:
    • Pointers should contain memory addresses, but this is not enforced
  • An Example:
    • ptr = &i;
  • An Illustration:
    • static-memory4
Pointing to Memory (cont.)
Back SMYC Forward
  • De-referencing a Pointer:
    • Finds the value of the entity that is being pointed at
    • Done with the * operator
  • An Example:
    • int j; j = *ptr;
  • An Illustration:
    • static-memory5
Pointing to Memory (cont.)
Back SMYC Forward
  • Multiple Pointers to the Same Memory:
    • Just as you can have two signs that point to the same airport, you can have two pointers that point to the same entity
  • An Example:
    • int *otherptr; otherptr = &i;
  • An Illustration:
    • static-memory6
Pointing to Memory (cont.)
Back SMYC Forward
  • De-referencing Revisited:
    • You can de-reference a pointer on the left-hand-side of an assignment operator
  • An Example:
    • (*otherptr) = 9;
  • An Illustration:
    • static-memory7
Arrays
Back SMYC Forward
  • Java vs. C++:
    • In Java, arrays are objects (and hence are reference types)
    • What happens in C++?
  • An Example:
    • int a[4];
  • An Illustration:
    • static-memory8
Arrays (cont.)
Back SMYC Forward
  • A Surprising Fact:
    • In most contexts, an unsubscripted array name represents a pointer to the first element of the array
    • So, in most contexts, a, &a and &a[0] all evaluate to the same thing
    • The exceptions are when the array name passes the entire array (e.g., with sizeof and &)
  • An Example:
    • cout << "The contents of a: " << a << "\n"; cout << "The address of a: " << &a << "\n"; cout << "The address of a[0]: " << &a[0] << "\n"; cout << "The address of a[1]: " << &a[1] << "\n"; cout << "The address of a[2]: " << &a[2] << "\n"; cout << "The address of a[3]: " << &a[3] << "\n";
  • A Note:
    • In the last four, [] is evaluated before &
Arrays (cont.)
Back SMYC Forward
  • Elements of Arrays:
    • An element of an int array is an int
  • An Example:
    • a[0] = 100; a[1] = 101; a[2] = 102; a[3] = 103;
  • An Illustration:
    • static-memory9
Arrays (cont.)
Back SMYC Forward
  • Pointers to Arrays:
    • A pointer to an element of an int array is a pointer to an int
  • An Example:
    • int *thirdptr; thirdptr = &a[0];
  • An Illustration:
    • static-memory10
Arrays (cont.)
Back SMYC Forward
  • Passing Arrays as Parameters:
    • When an array is passed as an actual parameter it is converted to the corresponding pointer type
  • Be Careful:
    • This means that the formal parameter (i.e., the parameter in the method declaration/definition) should be a pointer
Pointer Arithmetic
Back SMYC Forward
  • A Surprising Fact:
    • Adding 1 to a pointer to an int does not change its contents by 1, it changes its contents by the size of the data type it is pointing to
  • An Example:
    • thirdptr+1 is 0x22FF54
  • Using This Fact:
    • *(thirdptr) is the same as a[0]
    • *(thirdptr+1) is the same as a[1]
    • *(thirdptr+2) is the same as a[2]
    • *(thirdptr+3) is the same as a[3]
Arrays and Pointers
Back SMYC Forward

An Example

cppexamples/memory/IndexOf.cpp
 
References
Back SMYC Forward
  • Overview:
    • A reference serves as an indirect handle to an entity
    • References are declared using &
  • Differences Between Pointers and References:
    • A pointer may not address an entity whereas a reference always does
    • As a result, references must be initialized when declared
    • A reference cannot be reassigned to refer to another entity
References (cont.)
Back SMYC Forward
  • An Example:
    • int &r = i;
    • r now refers to i so it has the same address and the same contents
  • An Illustration:
    • static-memory11
References (cont.)
Back SMYC Forward
  • An Example:
    • r = 8;
    • A value of 8 is assigned to r and hence to i
  • An Illustration:
    • static-memory12
Objects
Back SMYC Forward

An Encapsulation of a Simple Weight

cppexamples/memory/Weight.h
 
cppexamples/memory/Weight.cpp
 
Objects (cont.)
Back SMYC Forward
  • An Example:
    • Weight w(6,11);
    • Memory is allocated for a Weight object, its constructor is called, and the two member attributes are initialized
  • An Illustration:
    • static-memory13
There's Always More to Learn
Back -