Professional Documents
Culture Documents
Programming Fundamentals
Lecture 2 & 3
Pointers
Fazeelat Mazhar
Department of Electrical Engineering
National University of Computing and Emerging Sciences, Chiniot Faisalabad Campus
Pointers
Why we need pointers
Why we need Pointers:
•Accessing array elements
•Passing arrays and strings as arguments
•Creating data structures such as linked lists
•Dynamic memory allocation, which can grow at runtime using.
Pointer variable (Chp 14 D.S Malik Chp 10 Lafore)
. We can either:
make it point to something that already exists,
ptr1 = &C
or ptr = NULL
ptr
? var1 ?
5 var2 ?
5
More C/C++ Pointer Dangers
Declaring a pointer just allocates space to hold the pointer – it does
not allocate something to be pointed to! Thus the address is NULL.
void f()
{
int *ptr;
*ptr = 5;
}
Pointers and Arrays
Arrays and pointers are closely related
Array name is like constant pointer
All arrays elements are placed in the consecutive locations. (This is only valid in
static memory allocation)
Example:- int List [10];
Pointers can do array subscripting operations (We can access array elements using
pointers).
Example:- int *ptr = List;
Relationship between Pointers and
Arrays
Accessing array elements with pointers
Assume declarations:
int List[5];
int *bPtr;
bPtr = List;
Effect:-
- List is an address, no need for &
- The bPtr pointer will contain the address of the first element of array
List.
Element List[ 2 ] can be accessed by *( bPtr + 2 )
Accessing 1-Demensional Array Using Pointers
Address Data
We know, Array name denotes the memory address of 980 Element 0
its first slot.
982 Element 1
Example:
984 Element 2
int List [ 50 ];
986 Element 3
int *Pointer;
988 Element 4
Pointer = List;
990 Element 5
Other slots of the Array (List [50]) can be accessed 992 Element 6
using by performing Arithmetic operations on Pointer. 994 Element 7
accessed using:- …
int *Value = Pointer + 3;
…
The value of (element 4th) can be accessed using:- 998 Element 50
int Value = *(Pointer + 3);
Array of pointers
#include <iostream>
using namespace std;
const int MAX = 4;
int main ()
{
char *names[MAX] = { "Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali", };
for (int i = 0; i < MAX; i++)
{
cout << "Value of names[" << i << "] = ";
cout << names[i] << endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Value of names[0] = Zara Ali Value of names[1] = Hina Ali Value of names[2] = Nuha Ali Value of names[3] =
Sara Ali
Arithmetic on Pointer
A pointer may be incremented or decremented.
This means only address in the pointer is incremented or decremented.
}
Logical operators on Pointers
We can apply logical operators (<, >, <=, >=, ==, != ) on
pointers.
But remember pointers can be compared to pointers or
NULL
Example (6)
int *pointer1, *pointer2; // both pointer2 contains NULL
addresses
int num1 = 93;
If ( pointer1 == NULL ) // pointer compared to NULL
pointer1 = &num1;
pointer2 = &num1;
If ( pointer1 == pointer2 ) // pointer compared to pointer
printf(“Both pointers are equal”);
Problem 1 Find output
void main ()
{
int a=10, *ptr;
ptr = &a;
Cout<<“value of a is”<<a<<endl;
*ptr = (*ptr)/2;
Cout<<“value of a is”<<(*ptr);
System(“pause”);
}
Answer: 10, 5
Problem 2
Find Output
Problem 3 Find Output ?
int main()
{
int a = 32, *ptr = &a;
char ch = 'A', &cho = ch;
cho += a;
*ptr += ch;
cout << a << ", " << ch << endl;
return 0;
}
Answer: 129,
a
Problem 4 Find Output
int main()
{
const int i = 20;
const int* const ptr = &i;
(*ptr)++;
int j = 15;
ptr = &j;
cout << i;
return 0;
}