Introduction To Programming: Engr. Rashid Farid Chishti

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20
At a glance
Powered by AI
Pointers are variables that store memory addresses rather than values. They allow access and modification of other variables. Key applications include data structures and passing arguments to functions.

Pointers are variables that store memory addresses rather than values. Every byte of memory has a unique address. A pointer variable contains the address of another variable. The address-of operator & returns the address of a variable. The value at an address can be accessed with the dereference operator *.

Pointer variables are declared with a type followed by an asterisk, such as int* ptr. A pointer is initialized by using the address-of operator on another variable, like ptr = &var. The value of the pointed variable can be accessed with the dereference operator like *ptr. Pointer arithmetic allows incrementing/decrementing the pointer.

Introduction to Programming

Engr. Rashid Farid Chishti


[email protected]
Chapter 10: Pointers

International Islamic University H-10, Islamabad, Pakistan


http://www.iiu.edu.pk

Pointers

Pointers are a special type of variables in


which a memory address is stored.
They contain a memory address, not the value
of the variable.
Pointers are an important and essential tool
for increasing the power of C++. A notable
example is the creation of data structures
such as linked lists and binary trees.
In fact, several key features of C++, such as
virtual functions, the new operator, and the
this pointer require the use of pointers.

Address and Pointers

The ideas behind pointers are not complicated.


Heres the first key concept: Every byte in
the computers memory has an address.
Addresses are numbers, just as they are for
houses on a street. The numbers start at 0 and
go up from there 1, 2, 3, and so on.
If you have 1KB of memory, the highest address
is 1023. (Of course you have much more.)
Your program, when it is loaded into memory,
occupies a certain range of these addresses.
That means that every variable and every
function in your program starts at a
Particular address.
You can find the address occupied by a
variable by using the address-of operator &.

Address and Pointers

The Address-of Operator &


// varaddr.cpp
// addresses of variables
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int var1 = 11; //define and initialize
char var2 = 'A'; //three variables
float var3 = 33.34;
cout << &var1 << endl //print the addresses
<< &var2 << endl //of these variables
<< &var3 << endl;
system("PAUSE"); return 0;
}

Pointer Variable
// ptrvar.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int var1 = 11; // two integer variables
int var2 = 22;
cout << &var1 << endl //print addresses of variables
<< &var2 << endl << endl;
int* ptr;
// pointer to integers
ptr = &var1;
// pointer points to var1
cout << ptr << endl; // print pointer value
ptr = &var2;
// pointer points to var2
cout << ptr << endl; // print pointer value
system("PAUSE"); return 0; }

Pointer Variable
// accessing the variable pointed to
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int var1 = 11;
// two integer variables
int var2 = 22;
int* ptr;
// pointer to integers
ptr = &var1;
// pointer points to var1
cout << *ptr << endl; // print contents of pointer (11)
ptr = &var2;
// pointer points to var2
cout << *ptr << endl; // print contents of pointer (22)
system("PAUSE");
return 0;
}

Pointer Variable
// other access using pointers
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int var1, var2;
// two integer variables
int* ptr;
// pointer to integers
ptr = &var1;
// set pointer to address of var1
*ptr = 37;
// same as var1 = 37
var2 = *ptr;
// same as var2 = var1
cout << var1 << endl; // verify var2 is 37
cout << var2 << endl; // verify var2 is 37
system("PAUSE");
return 0;
}

Pointer to void
// pointers to type void
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int intvar;
// integer variable
float flovar;
// float variable
int* ptrint;
// define pointer to int
float* ptrflo;
// define pointer to float
void* ptrvoid;
// define pointer to void
ptrint = &intvar; // ok, int* to int*
// ptrint = &flovar;
// error, float* to int*
// ptrflo = &intvar;
// error, int* to float*
ptrflo = &flovar;
// ok, float* to float*
ptrvoid = &intvar;
// ok, int* to void*
ptrvoid = &flovar;
// ok, float* to void*
system("PAUSE"); return 0; }

Pointers and Arrays


// array accessed with pointer notation
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
// array
int intarray[5] = { 31, 54, 77, 52, 93 };
for (int j=0; j<5; j++) // for each element,
cout << *(intarray+j) << endl; // print value
system("PAUSE");
return 0;
}

Pointer Arithmatic
// Adding and Subtracting a pointer variable
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int intarray[] = { 31, 54, 77, 52, 93 }; //array
int* ptrint;
// pointer to int
ptrint = intarray;
// points to intarray
cout << *ptrint << endl;
// print at index 0;
ptrint = ptrint + 2;
cout << *ptrint << endl;
// print at index 3;
ptrint++; cout << *ptrint << endl;
// print at index 4;
ptrint = ptrint-2;
cout << *ptrint << endl;
// print at index 1;
ptrint--; cout << *ptrint << endl;
// print at index 0;
system("PAUSE"); return 0;
}

Pointer and Functions


// arguments passed by pointer
#include <iostream>
#include <stdlib.h>
using namespace std;
void centimize(double*);
// prototype
int main()
{
double var = 10.0;
// var has value of 10 inches
cout << "var = " << var << " inches" << endl;
centimize(&var);
// change var to centimeters
cout << "var = " << var << " centimeters" << endl;
system("PAUSE"); return 0;
}
void centimize(double* ptrd) // store the address in ptdr
{
*ptrd *= 2.54;
// *ptrd is the same as var
}

Pointers to String Constants


// strings defined using array and pointer notation
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
// str1 is an address, that is a pointer constant
char str1[] = "Defined as an array" ;
// while str2 is a pointer variable. it can be changed
char* str2 = "Defined as a pointer" ;
cout << str1 << endl; // display both strings
cout << str2 << endl;
// str1++; // cant do this; str1 is a constant
str2++; // this is OK, str2 is a pointer
cout << str2 << endl; // now str2 starts efined...
system("PAUSE");
return 0;
}

Strings as Function Arguments


// displays a string with pointer notation
#include <iostream>
#include <stdlib.h>
using namespace std;
void dispstr(char*); // prototype
int main()
{
char str[] = "Idle people have the least leisure." ;
dispstr(str);
// display the string
system("PAUSE"); return 0;
}
void dispstr(char* ps){
while( *ps )
// until null ('\0') character,
cout << *ps++;
// print characters
cout << endl;
}

Copying a String Using Pointers


// copies one string to another with pointers
#include <iostream>
#include <stdlib.h>
using namespace std;
void copystr(char*, const char*); // function declaration
int main(){
char* str1 = "Self-conquest is the greatest victory." ;
char str2[80];
// empty string
copystr(str2, str1);
// copy str1 to str2
cout << str2 << endl;
// display str2
system("PAUSE"); return 0;
}
void copystr(char* dest, const char* src){
while( *src ){
// until null ('\0') character
*dest = *src ;
// copy chars from src to dest
dest++; src++ ;
// increment pointers
}
*dest = '\0' ;} // terminate dest

The const Modifier and Pointers

The use of the const modifier with pointer declarations can be


confusing, because it can mean one of two things, depending on
where its placed. The following statements show the two
possibilities:
const int* cptrInt;
// cptrInt is a pointer to constant int
int* const ptrcInt;
// ptrcInt is a constant pointer to int
In the first declaration, you cannot change the value of whatever
cptrInt points to, although you can change cptrInt itself.
In the second declaration, you can change what ptrcInt points to,
but you cannot change the value of ptrcInt itself.
You can remember the difference by reading from right to left, as
indicated in the comments.

The const Modifier and Pointers


#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
const int a = 5; int b = 10;
const int* cptrInt; //cptrInt is a pointer to constant int
// ptrcInt is a constant pointer to int
int* const ptrcInt = &b;
cptrInt = &a;
// *cptrInt = 25; // can not change a constant value
*ptrcInt = 100;
cptrInt = &b;
// ptrcInt = &a; // can not change address of pointer
cout << "*ptrcInt = " << *ptrcInt << endl;
cout << "*cptrInt = " << *cptrInt << endl;
system("PAUSE"); return 0; }

You might also like