1-Lecture-DSOOP-Pointers
1-Lecture-DSOOP-Pointers
Oriented Programming
Pointers
Dr Tahir Nawaz
Website: https://www.tahirnawaz.com
Email: tahir.nawaz@ceme.nust.edu.pk
Introduction
• Variables are memory locations that can be accessed by
their corresponding identifiers, i.e. their names. The
program therefore does not need to care about the
physical address of the data in memory; it simply uses
the identifier whenever it needs to refer to the variable.
return 0;
}
Declaration of pointers
• A pointer is a variable whose value is the address of
another variable, thereby essentially pointing to the
address of that other variable.
int main ()
{
int firstvalue, secondvalue;
int * mypointer;
mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is " << firstvalue << '\n';
cout << "secondvalue is " << secondvalue << '\n';
return 0;
}
Declaration of pointers (contd.)
• Example
// more pointers
#include <iostream>
using namespace std;
int main () {
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;
return 0;
}
Initialization of pointers
• Pointers can be initialized at the point when they are
declared:
int myvar;
int * myptr = &myvar;
return 0;
}
Null pointers (contd.)
• In order to check for a null pointer, we can do the
following:
if(ptr) // succeeds if p is not null
if(!ptr) // succeeds if p is null
• Thus, if all unused pointers are given the null value and
you avoid the use of a null pointer, you can avoid the
accidental misuse of an uninitialized pointer. Many times,
uninitialized variables hold some junk values and it
becomes difficult to debug the program.
Pointer arithmetic
• Since pointer is an address that is a numeric value, we
can perform arithmetic operations on a pointer just like
it’s done on a numeric value. The four arithmetic
operators we can use on pointers are: ++, --, +, and -
• For example, consider a pointer ptr that is of type int
and lets say it points to the address 1000. Assuming 32-
bit integers, if we do
ptr++;
• ptr will now point to the address 1004 thus pointing to
next integer. Likewise, if ptr points to a character with
an address 1000, then ptr++ will point to the location
1001 because next character will be available at 1001.
Pointer arithmetic (contd.)
• Incrementing a pointer
#include <iostream>
int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
return 0;
}
Pointer arithmetic (contd.)
• Decrementing a pointer
#include <iostream>
using namespace std;
const int MAX = 3;
int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
return 0;
}
Pointers and Arrays
• The concept of pointers and arrays is actually inter-
related. In fact, arrays work very much like pointers to
their first elements, and, actually, an array can always be
implicitly converted to the pointer of the proper type. For
example, consider these two declarations:
int myarray [20];
int * mypointer;
• The following assignment operation would be valid:
mypointer = myarray;
• But the following is not valid!
myarray = mypointer;
• This is because mypointer can be assigned a different
address, whereas myarray can never be assigned
anything, and will always represent the same block of 20
elements of type int.
Pointers and Arrays
• Example:
#include <iostream>
using namespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}
Array of pointers
• We can declare an array of pointers as follows:
int *ptr[size];
• The above statement declares an array of int type
pointers, each pointer points to a specific int type
variable
• See the following example:
#include <iostream>
int main () {
int var[MAX] = {10, 100, 200};
int *ptr[MAX];
Array of pointers (contd.)
for (int i = 0; i < MAX; i++) {
ptr[i] = &var[i]; // assign the address of
integer.
}
return 0;
}
Array of pointers (contd.)
• We can also use an array of pointers to character to
store a list of strings:
#include <iostream>
int main () {
const char *names[MAX] = { "Babar Azam", "Muhammad
Rizwan", "Asad Rauf", "Shaheen Afridi" };
return 0;
}
Pointers and constant
• As we have seen that pointers can be used to access a
variable by its address, and this access may include
modifying the value pointed. But it is also possible to
declare pointers that can access the pointed value to
read it, but not to modify it by declaring the pointer as
const.
int x;
int y = 10;
const int * p = &y;
x = *p; // ok: reading p
*p = x; // error: modifying p, which is const-
qualified
Pointer to pointer
• As we know, a pointer contains the address of a variable
it is pointing. When we define a pointer to a pointer, the
first pointer contains the address of the second pointer,
which points to the location that contains the actual value
as shown below.
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
return 0;
}
Passing pointers to functions
• Like other variables, we can pass pointers to functions.
This is done by simply declaring the function parameter
as a pointer type.
#include <iostream>
#include <ctime>
int main () {
unsigned long sec;
getSeconds( &sec );
return;
}
Passing pointers to functions (contd.)
• Example:
#include <iostream>
using namespace std;
// function declaration:
double getAverage(int *arr, int size);
int main () {
// an int array with 5 elements.
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
return avg;
}
Return pointer to functions
• To return a pointer from a function, we declare it as
follows:
int * myFunction();
return r;
}
Return pointer to functions (contd.)
// main function to call above defined function.
int main () {
// a pointer to an int.
int *p;
p = getArray();
for ( int i = 0; i < 10; i++ ) {
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}
return 0;
}
Acknowledgement/References
• https://www.tutorialspoint.com/cplusplus/index.htm
• https://www.cplusplus.com/