Professional Documents
Culture Documents
Chapter 3- Pointers
Chapter Objectives
Overview of pointers
Declaration and assignment of pointers
Pointers to pointers
Pointer Arithmetic
Arrays of Pointers
Pointers and Arrays
Dynamic Memory and Dynamic Arrays
Array and Pointers in Functions
Chapter 3- Pointers in C++
Pointers- Overview
When we declare a variable we inform the compiler of two things,
the name of the variable and the type of the variable. For
example, we declare a variable of type integer with the name k by
writing:
int k;
On seeing the "int" part of this statement the compiler sets aside 4
bytes of memory (on a PC) to hold the value of the integer.
It also sets up a symbol table. In that table it adds the symbol k and
the relative address in memory where those 4 bytes were set aside.
ptr = NULL;
guarantees that the pointer has become a null pointer.
Similarly, just as one can test for an integer value of zero, as in
if(k == 0), we can test for a null pointer using
int * p_age;
Whenever the dereference operator, *, appears in a variable
declaration, the variable being declared is always a pointer
variable.
Chapter 3- Pointers in C++
Assigning values to pointers
p_age is an integer pointer. The type of a pointer is very important.
p_age can point only to integer values, never to floating-point or other types.
To assign p_age the address of a variable:
int age = 26; int * p_age; p_age = &age;
OR
int age = 26; int * p_age = & age;
Two ways to print the value of age:
cout<<age;//prints value of age
cout<<*p_age;//dereferences p_age;
Chapter 3- Pointers in C++
Assigning a value to a dereferenced pointer
int j = 5;
int i = 5;
int *ptrj1 = &j;
int *ptrj2 = &j;
int *ptri = &i;
True/False:
o if (ptrj1 == ptrj2) ? True
o if (ptrj1 == ptri) ? False
o if (&ptrj1 == &ptrj2) ? False
o if (*ptrj1 == *ptri) ? True
Chapter 3- Pointers in C++
Assignment operator in Pointers
x some int
int *x;
int **y; y some *int some int
int numbers[20];
int * p;
the following allocation would be valid:
p = numbers;
Chapter 3- Pointers in C++
Pointers and Arrays Cont’d…
At this point p and numbers are equivalent and they have the same
properties, with the only difference that we could assign another value to
the pointer p whereas numbers will always point to the first of the 20
numbers of type int with which it was defined.
Therefore, although the previous expression was valid, the following
allocation is not:
numbers = p
Because numbers is an array (constant pointer), and no values can be
assigned to constant identifiers.
Chapter 3- Pointers in C++
Pointers and Arrays Cont’d…
Array name is basically a const pointer pointing at the
beginning of the array.
You can use the [ ] operator with pointers!
Example:
o int A[5];
o Creates a memory block of 5 integers on the stack (5x4bytes) where A
(the pointer) points at the beginning of the array -> A[0]. (A = &A)
A
Chapter 3- Pointers in C++
Pointers and Arrays Cont’d…
int *x;
int a[5]={-1,-2,-3,-4,-5};
x is “the address of a[2] ”
x = &a[2];
for (int i=0;i<3;i++)
x[i]++;
a x[i] is the same as a[i+2]
x
-1 -2 -3 -4 -5
x[0]
Chapter 3- Pointers in C++
x[1] x[2]
Array and pointers -Example
cout<<"*p="<<*p; //output=____
p++;
cout<<" *(++p)="<<*(++p); //pre increment output=____
cout<<" *p="<<*p; //output=___________________
Chapter 3- Pointers in C++
Dynamic Memory Allocation- Application of Pointers
Until now, in our programs, we have only had as much memory as we
have requested in declarations of variables, arrays and other objects
that we included, having the size of all of them to be fixed before the
execution of the program.
But, What if we need a variable amount of memory that can only be
determined during the program execution (runtime)? For example, in
case that we need a user input to determine the necessary amount of
space.
The answer is dynamic memory, for which C++ integrates the
operators new and delete.
Chapter 3- Pointers in C++
Dynamic Memory Allocation- Cont’d…
Pointers are useful for creating dynamic objects
during program execution.
o Unlike normal (global and local) objects which are
allocated storage on the runtime stack, a dynamic object
is allocated memory from a different storage area called
the heap.
o Dynamic objects do not obey the normal scope rules.
Their scope is explicitly controlled by the programmer.
Chapter 3- Pointers in C++
Variables in C++ Memory System
There are two "kinds" of memory available to a process:
Stack:
o Stores local variables of the function.
o Removed once the function ends!
o Used by functions, global and local variables in C++
Heap:
o Contains dynamically allocated variables.
o Stays once the function ends, unless cleared before(explicitly)!
o Used by pointers
//and
p
int *p= new int; Nameless
p variable
*p=10; 10