Professional Documents
Culture Documents
int j = 99;
iPtr = &j; // You can change the
Prepared address
by Nega Teferra (M.Techstored
on CSE) in a pointer
Uninitialized Pointers
The following code fragment has a serious logical error!
int * iPtr;
*iPtr = 55;
cout << *iPtr << endl;
The pointer iPtr was declared without initialization.
i.e., it is pointing to "somewhere" which is of course an
invalid memory location.
The *iPtr = 55 corrupts the value of "somewhere"!
You need to initialize a pointer by assigning it a valid
address.
Most of the compilers does not signal an error or a warning
for uninitialized pointer?!
Prepared by Nega Teferra (M.Tech on CSE)
Null Pointers
You can initialize a pointer to 0 or NULL.
i.e., it points to nothing. It is called a null pointer.
Dereferencing a null pointer (*p) causes an
STATUS_ACCESS_VIOLATION exception.
int * iPtr = 0; // Declare an int pointer, and initialize the
pointer to point to nothing
cout << *iPtr << endl; // ERROR! STATUS_ACCESS_VIOLATION
exception
int * p = NULL; // Also declare a NULL pointer points to
nothing
Initialize a pointer to null during declaration is a good software
engineering practice
Prepared by Nega Teferra (M.Tech on CSE)
Pointers and Arrays
Arrays and pointers work based on a related concept.
There are different things to note when working with
arrays having pointers.
The array name itself denotes the base address of the
array.
This means that to assign the address of an array to a
pointer, you should not use an ampersand (&).
We can implicitly convert an array into a pointer. For example:
int *ip;
int arr[] = { 10, 34, 13, 76, 5, 46 };
ip = arr;
for (int x = 0; x < 6; x++)
{
cout << *ip << “ ,”;
ip++;
}
Prepared by Nega Teferra (M.Tech on CSE)
Cont.… It is perfectly acceptable to apply the
pointer operator * to var but it is
illegal to modify var value.
const int MAX = 3;
int main () The reason for this is that var is a
{ constant that points to the beginning
int var[MAX] = {10, 100, 200}; of an array and can not be used as l-
for (int i = 0; i < MAX; i++) value.
{ Because an array name generates a
*var = i; // This is a correct syntax pointer constant, it can still be used in
var++; // This is incorrect. pointer-style expressions, as long as it
} is not modified.
return 0;
}
*var=var[0]
*(var+1)=var[1]
*(var + 2) = var[2];
str ptr
str++ ptr++
Prepared by Nega Teferra (M.Tech on CSE)
Incrementing and decrementing a Pointer
We prefer using a pointer in our program instead of an array because
the variable pointer can be incremented, unlike the array name which
cannot be incremented because it is a constant pointer.
The same considerations apply to decrementing a pointer, which
decreases its value by the number of bytes of its data type
int main () int main ()
{ {
int var[MAX] = {10, 100, 200}; int var[MAX] = {10, 100, 200};
int *ptr; int *ptr;
ptr = var; ptr = &var[MAX-1];
for (int i = 0; i < MAX; i++) for (int i = MAX; i > 0; i--)
{ {
cout << "Value of var[" << i << "] = "; cout << "Value of var[" << i << "] = ";
cout << *ptr << endl; cout << *ptr << endl;
ptr++; ptr--;
} }
} } Prepared by Nega Teferra (M.Tech on CSE)
Comparing Pointers
Pointers may be compared by using any of C++’s relational
operators:
>, <, ==, !=, >=, <=
If one address comes before another address in memory,
the first address is considered “less than” the second.
This two are different
if (ptr1 < ptr2) // Comparing the Address
if (*ptr1 < *ptr2) //Comparing the pointed value
int main()
{
int var =30;
int *ptr,**pptr;
ptr = &var;
pptr = &ptr;
cout << "Value of var :" <<var<< endl;
cout << "Value available at *ptr :"<<*ptr<<endl;
cout << "Value available at **pptr :" <<**pptr<< endl;
return 0;
}