Lecture 03 - Indiana University South Bend

Learning Objectives
 Pointers
 * symbol and & symbol
 Pointer operations
 Pointer in function call
 Application: exchange values of two
variables
 Pointer and array
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-1
Pointer Introduction
 Pointer definition:
 Memory address of a variable
 Recall: memory divided
 Numbered memory locations
 Variable name is used as address
 You’ve used memory address already!
 Call-by-reference
 Address of actual argument was passed
 Pass array in a function call
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-2
Pointer Variables
 Pointers are "typed"
 Can store addresses in variable
 Pointers to int, double, etc.!
 Example:
double *p;
 p is declared a "pointer to double" variable
 Can hold pointers to variables of type double
 Not other types!
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-3
Declaring Pointer Variables
 Pointers declared like other types
 Add "*" before variable name
 Produces "pointer to" that type
 "*" must be before each variable
 int *p1, *p2, v1, v2;
 p1, p2 hold pointers to int variables
 v1, v2 are ordinary int variables
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-4
Pointing to …
 int *p1, *p2, v1, v2;
p1 = &v1;
 Sets pointer variable p1 to "point to" int
variable v1
 Operator, &
 Determines "address of" variable
 Read like:
 "p1 equals address of v1"
 Or "p1 points to v1"
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-5
Pointing to …
 Recall:
int *p1, *p2, v1, v2;
p1 = &v1;
 Two ways to refer to v1 now:
 Variable v1 itself:
cout << v1;
 Via pointer p1:
cout << *p1;
 Dereference operator, *
 Pointer variable "dereferenced"
 Means: "Get data that p1 points to"
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-6
"Pointing to" Example
 Consider:
v1 = 0;
p1 = &v1;
*p1 = 42;
cout << v1 << endl;
cout << *p1 << endl;
 Produces output:
42
42
 *p1 and v1 refer to same variable
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-7
& Operator
 The "address of" operator
 Also used to specify call-by-reference
parameter
 Similar mechanism
 Call-by-reference parameters pass
"address of" the actual argument
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-8
Pointer Assignments
 Pointer variables can be "assigned":
int *p1, *p2;
p2 = p1;
 Assigns one pointer to another
 "Make p2 point to where p1 points"
 Do not confuse with:
*p1 = *p2;
 Assigns "value pointed to" by p1, to "value
pointed to" by p2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-9
Pointer Assignments Graphic:
Display 10.1 Uses of the Assignment
Operator with Pointer Variables
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-10
Define Pointer Types
 Can "name" pointer types
 To be able to declare pointers like other
variables
 Eliminate need for "*" in pointer declaration
 typedef int* IntPtr;
 Defines a "new type" alias
 Consider these declarations:
IntPtr p;
int *p;
 The two are equivalent
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-11
Pointers and Functions
 Recall
 Pass by value
 Pass by reference
 Pointers can be function parameters
 Behaves like pass by reference
 Example: next slide
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-12
void main()
{
int m = 77;
int * p = &m;
cout << "m = " << m << endl;
sneaky(p);
cout << "m = " << m << endl;
}
void sneaky(int * temp)
{
*temp = 99;
}
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-13
Call-by-value Pointers Graphic: The
Function Call sneaky(p);
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-14
Application of Pointer
 Exchange values of two variables
 Pass by value: won’t work
 Pass by reference: will work
 Pass by pointer: will work
 Pass by pointer to pointer: what is this?
10-15
Pointer and array
 Both array and pointer are pass by
reference in a function call
 Both array and pointer refer to some
physical memory address
 So, in C++, pointers can be used to
represent arrays
 Any type of array (int, double, char, …)
Copyright © 2006 Pearson Addison-Wesley. All rights reserved.
10-16
Pointer and array (continued)
double a[4] = {1.1, 2.2, 3.3, 4.4};
double f = 5.5;
double *p;
p = &f;
p = a; // or p = & a[0];
// now p can be used same as a to access array
for(int i=0; i<4; i++)
cout << p[i] << "\t";
cout << endl;
10-17
Pointer and array (continued)
 So, we can use pointer to represent
array, even in a function call
double a[4] = {1.1, 2.2, 3.3, 4.4};
double *p = a;
reset(p, 4); //or reset(a, 4);
for(int i=0; i<4; i++)
cout << p[i] << "\t";
void reset(double * x, int size)
{
for(int i=0; i<size; i++)
}
x[i]=5.5;
10-18
Pointer and C-String
 C-String is a character array, so pointer
can be used to represent C-string
 What will be displayed?
char str[] = "cs201";
cout << str << endl;
cout << "str contains " << strlen(str) << " characters" << endl;
char *p;
p = str; // p = &str[0];
cout << p << endl;
cout << "p contains " << strlen(p) << " characters" << endl;10-19
Pointer and C-String (continued)
 What should be displayed?
char *p = "USA";
char *q = "Indiana";
q = p;
reset(q);
cout << "q=" << q << endl;
void reset(char * x)
{
x="IU South Bend";
}
10-20
Pointer and Array and C-String
 Assigment can not be performed in array but can be
done in pointers
int m[3] = {1, 2, 3};
int n[3] = {-1, -2, -3};
m = n; // This is NOT allowed
int * p1 = m;
int * p2 = n;
p1 = p2; // This is allowed
10-21
Pointer and Array and C-String
(continued)
 Assigment can not be performed in C-string but can be
done in pointers
char m[] = “Hello”;
char n[] = “South Bend”;
m = n; // This is NOT allowed
char * p1 = m;
char * p2 = n;
p1 = p2; // This is allowed
10-22