0% found this document useful (0 votes)
9 views

Pointers Part 1

The document discusses pointers in C++. It defines a pointer as a special variable that holds the address of another variable. It provides examples of integer and character pointers. It compares pointers to arrays, explaining that an array name without indices points to the address of the first index. The document also covers the new keyword, which is used to allocate memory dynamically for pointers and arrays at runtime.

Uploaded by

Inshal Yousaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Pointers Part 1

The document discusses pointers in C++. It defines a pointer as a special variable that holds the address of another variable. It provides examples of integer and character pointers. It compares pointers to arrays, explaining that an array name without indices points to the address of the first index. The document also covers the new keyword, which is used to allocate memory dynamically for pointers and arrays at runtime.

Uploaded by

Inshal Yousaf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Object Oriented Programming

Pointers

Mr. Usman Wajid

[email protected]

Usman Wajid Object Oriented Programming Pointers 1 / 10


What is a Pointer?

Pointer
It is special type of variable that holds the address of a conventional (simple) variable as a
value

• Basic Syntax:

< data - type > * < identifier > = & < identifier >;

Usman Wajid Object Oriented Programming Pointers 2 / 10


Integer Pointer Example:

int main () {

int x = 5;
int * p = & x ;

cout < < " value of x : " <<x < < endl ;
cout < < " Address of x : " < <&x < < endl ;
cout < < " value of p : " <<p < < endl ;
cout < < " Address of p : " < <&p < < endl ;
cout < < " value of * p : " < <*p < < endl ;
}

Usman Wajid Object Oriented Programming Pointers 3 / 10


Integer Pointer Example:

int main () {

int x = 5;
int * p = & x ;

cout < < " value of x : " <<x < < endl ;
cout < < " Address of x : " < <&x < < endl ;
cout < < " value of p : " <<p < < endl ;
cout < < " Address of p : " < <&p < < endl ;
cout < < " value of * p : " < <*p < < endl ;
}

Usman Wajid Object Oriented Programming Pointers 3 / 10


Integer Pointer Example:

int main () {

int x = 5;
int * p = & x ;

cout < < " value of x : " <<x < < endl ;
cout < < " Address of x : " < <&x < < endl ;
cout < < " value of p : " <<p < < endl ;
cout < < " Address of p : " < <&p < < endl ;
cout < < " value of * p : " < <*p < < endl ;
}

Usman Wajid Object Oriented Programming Pointers 3 / 10


Char Pointer Example:

int main () {

char c = ’a ’;
char * p = & c ;

cout < < " value of c : " <<c < < endl ;
cout < < " Size of c : " << sizeof ( c ) << endl ;
cout < < " value of * p : " < <*p < < endl ;
cout < < " Size of p : " << sizeof ( p ) << endl ;
}

Usman Wajid Object Oriented Programming Pointers 4 / 10


Char Pointer Example:

int main () {

char c = ’a ’;
char * p = & c ;

cout < < " value of c : " <<c < < endl ;
cout < < " Size of c : " << sizeof ( c ) << endl ;
cout < < " value of * p : " < <*p < < endl ;
cout < < " Size of p : " << sizeof ( p ) << endl ;
}

Usman Wajid Object Oriented Programming Pointers 4 / 10


Pointer Vs Array

An Array name without indices


It points to the address of the first index of the array

Usman Wajid Object Oriented Programming Pointers 5 / 10


Pointer Vs Array Example:
int main () {
int array [3] = {2 , 5 , 7};
int * p ;
cout < < " & array = " < <& array < < endl ;
cout < < " array = " << array < < endl ;
cout < < " & array [0] = " < <& array [0] < < endl ;
cout < < " array [0] = " << array [0] < < endl ;
cout < < " & array [1] = " < <& array [1] < < endl ;
cout < < " array [1] = " << array [1] < < endl ;
cout < < " & array [2] = " < <& array [2] < < endl ;
cout < < " array [2] = " << array [2] < < endl ;
cout < < " \ np = array " << endl ; p = array ;
cout < < " \ np = " <<p < < endl ;
cout < < " * p = " < <*p < < endl ;
cout < < " \ np ++; " << endl ; p ++;
cout < < " p = " <<p < < endl ;
cout < < " * p = " < <*p < < endl ;
cout < < " \ np ++; " << endl ; p ++;
cout < < " p = " <<p < < endl ;
cout < < " * p = " < <*p < < endl ;
}

Usman Wajid Object Oriented Programming Pointers 6 / 10


The new Keyword

The new Keyword


It is used to store a value in the memory that can be accessed using a pointer only. It is also
used to allocate the size an array size dynamically during the run time.

1 Single Pointer Syntax:


< data - type > * < identifier > = new < data - type >;

2 Array Pointer Syntax:


< data - type > * < identifier > = new < data - type >[ < array - size >];

Usman Wajid Object Oriented Programming Pointers 7 / 10


The new Keyword Example 1

int main () {
int * x = new int ;

*x = 1 ;

cout < < " \ n & x = " < <&x < < endl ;
cout < < " x = " <<x < < endl ;
cout < < " * x = " < <*x < < endl ;
}

Usman Wajid Object Oriented Programming Pointers 8 / 10


The new Keyword Example 2

int main () {
int * array = new int [3];

array [0] = 2;
array [1] = 5;
array [3] = 7;

cout < < " & array = " < <& array < < endl ;
cout < < " array = " << array < < endl ;
cout < < " & array [0] = " < <& array [0] < < endl ;
cout < < " array [0] = " << array [0] < < endl ;
cout < < " & array [1] = " < <& array [1] < < endl ;
cout < < " array [1] = " << array [1] < < endl ;
cout < < " & array [2] = " < <& array [2] < < endl ;
cout < < " array [2] = " << array [2] < < endl ;

}
Usman Wajid Object Oriented Programming Pointers 9 / 10
The new Keyword Example 3

int main () {
int * array = new int ;

* array = 2;
cout < < " array = " << array < < endl ;
cout < < " * array = " < <* array < < endl ;

array ++;
* array = 5;
cout < < " array = " << array < < endl ;
cout < < " * array = " < <* array < < endl ;

array ++;
* array = 7;
cout < < " array = " << array < < endl ;
cout < < " * array = " < <* array < < endl ;

} Usman Wajid Object Oriented Programming Pointers 10 / 10

You might also like