Chapter-3-Pointers in CPP (1) Up
Chapter-3-Pointers in CPP (1) Up
Chapter-3-Pointers in CPP (1) Up
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++ 2
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.
Chapter 3- Pointers in C++ 3
Pointers – Overview cont’d…
Thus, later if we write:
k = 2; we expect that, at run time when this statement is executed,
the value 2 will be placed in that memory location reserved for the
storage of the value of k.
In C++ we refer to a variable such as the integer k as an "object".
In a sense there are two "values" associated with the object k.
One is the value of the integer stored there (2 in the above example)
and the other the "value" of the memory location, i.e., the address of k.
But , sometimes we may be interested to have a variable that can hold
the address of another variable for so many reasons.
o Such as to have more control over the management of the computer’s memory, dynamic
arrays etc.
o Some data structures use pointers (e.g. linked list, tree).
Chapter 3- Pointers in C++ 4
Pointers – Overview Cont’d…
Such a variable is called a pointer variable (for reasons which
hopefully will become clearer a little later).
In C++ when we define a pointer variable we do so by preceding
its name with an asterisk *.
In C++ we also give our pointer a type which, in this case, refers to
the type of data stored at the address we will be storing in our
pointer.
For example, consider the variable declaration:
int y;
int *yptr; //ptr will store the address of an integer such as y
yptr=&y;
Chapter 3- Pointers in C++ 5
Operators in C++ Pointer
There are two pointer operators in C++:
o & the address of operator
o * the dereference operator
The & literally mean “address of.” always produces the memory
address of whatever it precedes.
o Is also called the reference operator.
The * operator, when used with pointers, either declares a
pointer or dereferences the pointer’s value.
The dereference operator can be literally translated to "value
pointed by" .
Chapter 3- Pointers in C++ 6
C++ Pointer Operators Cont’d..
Recall that a pointer is simply the address of an object in
memory.
Hence, generally, objects can be accessed in two ways:
o directly by their symbolic name, or
o indirectly through a pointer.
The act of getting to an object via a pointer to it, is called
dereferencing the pointer.
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++ 15
Assignment operator in Pointers
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++ 24
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++ 25
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++ 26
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] 27
Array and pointers -Example
cout<<"*p="<<*p; //output=____
p++;
cout<<" *(++p)="<<*(++p); //pre increment output=____
cout<<" *p="<<*p; //output=___________________
Chapter 3- Pointers in C++ 33
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.
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
int *p;
for (int i = 0; i < 10; ++i) {
p = getNumbers(); r[i] = i+2;
for ( int i = 0; i < 10; i++ ) { cout << r[i] << endl;
cout << "*(p + " << i << ") : "; }
cout << *(p + i) << endl; return r;
}
}
CHAPTER 3- POINTERS IN C++ 56
Returning array of characters- another example
The following code in the main can be A function that returns a pointer
used to call the function. to nameless array of chars
int n;
cout<<"How many chars?"<<endl; char * getChars()
cin>>::n; {
char *chars=getChars(); char *p= new char[::n];
for (int y=0;y<::n;y++) for(int j=0;j<::n;j++)
{ {
cout<<*(chars+y)<<“ = "<<chars[y]<<endl; cin>>p[j];
} }
return p;
}
CHAPTER 3- POINTERS IN C++ 57
Pointers as function parameters
We can also create functions that take a Int main ()
pointer as an argument {
Write a function that takes a pointer to an array int x;
of integers and returns the sum of the numbers cout<<"Dynamic Array"<<endl;
int sum ( int *p, int numvars ) cout<<"how many elements do you need?"<<endl;
{ cin>>x;
int sum=0; int *arr = new int[x];
for (int k=0; k<numvars; k++) for( int j=0;j<x;j++)
{
{
arr[j]=j+10;
sum+=*(p+k);
}
}
cout<<" The sum of the numbers/elements
return sum; created is="<<sum(arr,x);
} return 0;
}
CHAPTER 3- POINTERS IN C++ 58
The End