lec week1 pointers
TRANSCRIPT
-
7/29/2019 Lec Week1 Pointers
1/55
Introduction to Pointers Every variable has three major item
associated with it:
Data Type of the variable
Actual Value Stored in the Variable
The Address of the Variable
-
7/29/2019 Lec Week1 Pointers
2/55
Introduction to Pointers Pointers are special type of variables in
which a memory address is stored.
They contain memory address and not the
value of the variable
-
7/29/2019 Lec Week1 Pointers
3/55
Introduction to Pointers For example we have declared a
variable
int x In this case a name x has been associated
with a memory location
We can have the memory address of x, say
6000 or whatever it is
-
7/29/2019 Lec Week1 Pointers
4/55
Introduction to Pointers
int *p
Means a location P which can contain
address of an integer In other word P can point towards an
integer variable (hence given the
name Pointer)
-
7/29/2019 Lec Week1 Pointers
5/55
Declaration of Pointers
datatype *X
X is name of Pointer
Datatype is the type of type to whichX will point
Each variable that is declared as
pointer must be preceded by an *
-
7/29/2019 Lec Week1 Pointers
6/55
Example
int x=14, *P;//P is a pointer
? 14
p=&x; //P contains address of x
&x 14
xP
P x
-
7/29/2019 Lec Week1 Pointers
7/55
Direct vs. Indirect Reference
Normally a variable contains a
specific value
Pointers contain address of variable So a variable name directly reference
a value and a pointerindirectly
references a value
-
7/29/2019 Lec Week1 Pointers
8/55
Dereference Operator
To get the value stored at the memory
address we use dereference
operator(*)
* is used with the name of the pointer
to get the value stored at the address
-
7/29/2019 Lec Week1 Pointers
9/55
-
7/29/2019 Lec Week1 Pointers
10/55
Note that address ofa and value ofptrare
identical
& and * are inverses of each other----whenthey are applied to ptr, consecutively (in
any order) the same result is printed.
-
7/29/2019 Lec Week1 Pointers
11/55
Initialization of Pointers
Pointers should be initialized either
when they are declared or in an
assignment statement A pointer may be initialized to 0,
NULL or to a address
Pointer initialized with 0 orNULLpoints to nothing
-
7/29/2019 Lec Week1 Pointers
12/55
Review:Calling Functions By Value
-
7/29/2019 Lec Week1 Pointers
13/55
Calling Functions By References
-
7/29/2019 Lec Week1 Pointers
14/55
Arrays are always passed by Ref
Arrays are always passed by
Reference
Arrays are not passed using & Name of the array is the address of
starting location in the memory of the
array The name of array is equivalent to
&Array[0]
-
7/29/2019 Lec Week1 Pointers
15/55
Pointer Expressions and Pointer
Arithmetic
Pointers are valid operands inarithmetic expressions, assignmentexpressions and comparisonexpressions
Howevernot all the operators used inthese expressions are valid for
pointer variables A limited set of arithmetic operations
may be performed on pointers
-
7/29/2019 Lec Week1 Pointers
16/55
A pointer may be
incremented or decremented (++ and --)
An integer may be added to a pointer(+ or+=)
An integer may be subtracted from a
pointer(- or -=)
One pointer may be subtracted fromanother
-
7/29/2019 Lec Week1 Pointers
17/55
Arrays are always passed by Ref
Arrays are always passed by
Reference
Arrays are not passed using & Name of the array is the starting
location in the memory of the array
The name of array is equivalent to&Array[0]
-
7/29/2019 Lec Week1 Pointers
18/55
Pointer Expressions and Pointer
Arithmetic
Pointers are valid operands inarithmetic expressions, assignmentexpressions and comparisonexpressions
Howevernot all the operators used inthese expressions are valid for
pointer variables A limited set of arithmetic operations
may be performed on pointers
-
7/29/2019 Lec Week1 Pointers
19/55
A pointer may be
incremented or decremented (++ and --)
An integer may be added to a pointer(+ or+=)
An integer may be subtracted from a
pointer(- or -=)
One pointer may be subtracted fromanother
-
7/29/2019 Lec Week1 Pointers
20/55
Pointer Arithmetic
int *y = &x;
y = y+1;
The y = y+1 will set y to the address ofthe next integer after x
If x has address 0, then it uses bytes 0,
1, 2, 3. Thus y gets 0, and then changesto 4 (not 1)
-
7/29/2019 Lec Week1 Pointers
21/55
Relationship between Arrays
and Pointers
When we declare array as
int y[10]
This means that we have reservedmemory spaces for ten integers and
named it collectively as y
Y represents the memory address ofthe beginning of this collective
memory space
-
7/29/2019 Lec Week1 Pointers
22/55
Relationship between Arrays
and Pointers
When we declare array as
int y[10]
The first element of array can beaccessed as y[0]
Memory address of first element i.e.
y[0] is stored in y
-
7/29/2019 Lec Week1 Pointers
23/55
Relationship between Arrays
and Pointers
The name of array is a constant
pointer which contains the memoryaddress of the first element of the
array
The difference between this and anordinary pointer is that the array
name is a constant pointer
-
7/29/2019 Lec Week1 Pointers
24/55
Relationship between Arrays
and Pointers
The difference between this and an
ordinary pointer is that the array
name is a constant pointer It means that array name will always
point to start of the array
It can not be reassigned to any otheraddress
-
7/29/2019 Lec Week1 Pointers
25/55
Example
int y[10];
int *yptr;
yptr=y;
Now we have two things pointing to the
same place (y and yptr)
Both are pointing towards first element
of the array
-
7/29/2019 Lec Week1 Pointers
26/55
Example
int y[10],x;
int *yptr=&x;
y=yptr;
-
7/29/2019 Lec Week1 Pointers
27/55
Pointer Arithmetic and
Expressions
What is an array?
A sequence of consecutive memory
blocks, each the size needed to hold thedata type
Consider the variable declaration
int arr[10]
This is a sequence of 40 bytes four for
each of the 10 integers
In truth, arris of type int*, with the same
address as arr[0]
-
7/29/2019 Lec Week1 Pointers
28/55
Since arris an int*, what is arr+1?
The address of the next integer (four bytes
forward) which is arr[1]
In general, arr+i is the memory
address ofarr[i]
Thus *(arr+i) is the value in arr[i]
Pointer Arithmetic and Arrays
-
7/29/2019 Lec Week1 Pointers
29/55
A picture of int arr[x]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Assuming arr==0 (it holds the address 0):
arr[0] is at address 0 which is equal to arr
arr[1] is at address 4 which is equal to arr+1
arr[2] is at address 8 which is equal to arr+2
arr[3] is at address 12 which is equal to arr+3
arr[4] is at address 16 which is equal to arr+4
-
7/29/2019 Lec Week1 Pointers
30/55
Example
int main() {
int arr[20];
int i;
for (i=0; i < 20; i++) {
*(arr+i) = 20-i; //Assigns values to the array}
for (i=0; i < 20; i++) {
cout
-
7/29/2019 Lec Week1 Pointers
31/55
Explanation
The example prints all numbers from0 to 19
In the example, we access theelements in two different ways: In the first loop, we used pointer arithmetic
In the second loop, use the [ ] operator
They do the same thing we could haveused either method for either loop
-
7/29/2019 Lec Week1 Pointers
32/55
Another Example
-
7/29/2019 Lec Week1 Pointers
33/55
When a Pointer is
incremented it actually jumpsthe number of memory
spaces according to the data
type that it points to
-
7/29/2019 Lec Week1 Pointers
34/55
Does this make any sense??
-
7/29/2019 Lec Week1 Pointers
35/55
void*
We can have pointers to any type
including void
This is useful as a generic type itallows us to abstract data types
We will not go into the detail of what is an
ADT
-
7/29/2019 Lec Week1 Pointers
36/55
Warning!
You can use the expression ptr++ for
a pointer variable
ButYou cannot use the ++ operation if the
pointer is the name of an array
It is a constant pointer and can't bechanged.
-
7/29/2019 Lec Week1 Pointers
37/55
Consider the following: int* p = 20;
*p = 0;
What did you just do?
Reset bytes 20, 21, 22 and 23, each, to 0.
What was at these bytes?
Who knows it could be important!!
Maybe it was some other variable you are using
Maybe it has something to do with the hard drive
It is theoretically possible to crash the system
-
7/29/2019 Lec Week1 Pointers
38/55
Accessing 2-D arrays with
pointers
-
7/29/2019 Lec Week1 Pointers
39/55
Dynamic Memory Allocation
Declaring Array
int Array[25]
What about this
int n=5;
Int Array[n]
Array size has to be a constant
-
7/29/2019 Lec Week1 Pointers
40/55
Need
What if you need to specify array size
when program is executing (Run-
time)
You will also need this in your project
-
7/29/2019 Lec Week1 Pointers
41/55
Dynamic Memory Management
with Operators new and delete
Dynamic memory management
Control allocation and deallocation of
memory
Operators newand delete
-
7/29/2019 Lec Week1 Pointers
42/55
Dynamic Memory Managementwith Operators new and delete
new
Consider
int *Ptr;
Ptr = new int;
new operator
Creates variable of proper size for type int
Returns pointer of specified type
Allocating arraysint *gradesArray = new int[ 10 ];
-
7/29/2019 Lec Week1 Pointers
43/55
Dynamic Memory Managementwith Operators new and delete
delete
Destroy dynamically allocated object and
free space
Consider
delete Ptr;
Operatordelete
Deallocates memory
De-allocating arrays
delete [] gradesArray;
Deallocates array to which gradesArray points
-
7/29/2019 Lec Week1 Pointers
44/55
What about 2-D Array
-
7/29/2019 Lec Week1 Pointers
45/55
Pointers to Pointers
We use double dereference to access
the elements of a 2D array by using
Array name (a pointer) to access arow (another pointer) and further to
access a column element
In case of single dereference, the
value of pointer is the address of the
variable that contains the value
desired
-
7/29/2019 Lec Week1 Pointers
46/55
Double Indirection
Pointer 2 Pointer1 Variable
Address ofPointer 2
Address ofVariable
Value
-
7/29/2019 Lec Week1 Pointers
47/55
Earlier we used arrays and pointers
interchangeably
We can think a pointer to pointer islike a pointer to a group of arrays
because a pointer itself can be
considered as an array
-
7/29/2019 Lec Week1 Pointers
48/55
Example
Suppose we have a group of
character strings
We can store them in a 2D array If we use conventional 2D array like
Name[5][10] we can store 5 strings
each of length 9 characters.
-
7/29/2019 Lec Week1 Pointers
49/55
Arrays can contain pointers
Commonly used to store array of strings
char *suit[ 4 ] = {"Hearts", "Diamonds",
"Clubs", "Spades" };
Each element ofsuit points to char * (a string)
Array does not store strings, only pointers to
strings
suit array has fixed size, but can point to stringsof any size
Arrays of Pointers
-
7/29/2019 Lec Week1 Pointers
50/55
-
7/29/2019 Lec Week1 Pointers
51/55
Arrays of Pointers
-
7/29/2019 Lec Week1 Pointers
52/55
-
7/29/2019 Lec Week1 Pointers
53/55
-
7/29/2019 Lec Week1 Pointers
54/55
More Examples
void main()
{
char *Name[15];
int Total;
cout
-
7/29/2019 Lec Week1 Pointers
55/55
Thank You