Pointers and References in C/C++
An Introduction
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
Motivation for Java Programmers
- 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
- A Line of Code:
- 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?
- An Illustration:
Variables: Their Names, Values and Addresses (cont.)
- Another Line of Code:
- What Happens:
-
i
is assigned the value 5
- An Illustration:
Pointing to Memory
- Declaring a Pointer:
- Pointers are declared using
*
- An Example:
- Where is the Memory?
- Use
&ptr
- (How much memory can be addressed in C++?)
- An Illustration:
Pointing to Memory (cont.)
- Using a Pointer:
- Pointers should contain memory addresses, but this
is not enforced
- An Example:
- An Illustration:
Pointing to Memory (cont.)
- De-referencing a Pointer:
- Finds the value of the entity that is being pointed at
- Done with the
*
operator
- An Example:
- An Illustration:
Pointing to Memory (cont.)
- 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:
- An Illustration:
Pointing to Memory (cont.)
- De-referencing Revisited:
- You can de-reference a pointer on the left-hand-side
of an assignment operator
- An Example:
- An Illustration:
Arrays
- Java vs. C++:
- In Java, arrays are objects (and hence are reference types)
- What happens in C++?
- An Example:
- An Illustration:
Arrays (cont.)
- 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:
- A Note:
- In the last four,
[]
is evaluated before
&
Arrays (cont.)
- Elements of Arrays:
- An element of an
int
array is an int
- An Example:
- An Illustration:
Arrays (cont.)
- Pointers to Arrays:
- A pointer to an element of an
int
array is a pointer to an int
- An Example:
- An Illustration:
Arrays (cont.)
- 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
- 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:
- 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
An Example
cppexamples/memory/IndexOf.cpp
References
- 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.)
- An Example:
-
int &r = i;
-
r
now refers to i
so
it has the same address and the same contents
- An Illustration:
References (cont.)
- An Example:
-
r = 8;
- A value of 8 is assigned to
r
and hence to
i
- An Illustration:
Objects
An Encapsulation of a Simple Weight
cppexamples/memory/Weight.h
cppexamples/memory/Weight.cpp
Objects (cont.)
- 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:
There's Always More to Learn