You are on page 1of 25

CS-214

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)

•A variable whose content is an address (that is, a memory address).


•When a pointer variable is declared, specify the data type of the value to be stored in the
memory location pointed to by the pointer variable.
•A pointer variable is declared by using the asterisk symbol (*) between the data types
and the variable name as shown,
dataType *identifier;
int *p;
char *ch;
•How to store address in a pointer?
Address of Operator (&), Referencing
•A unary operator that returns the address of its operand.
•For any integer x and pointer p,
p = &x;
Example:
int *p;
int num;
num = 78;
p = #
Dereferencing Operator (*)
•A unary operator, *, referred to as the dereferencing operator or indirection operator,
refers to the object to which its operand (that is, the pointer) points.
int x = 25;
int *p;
p = &x; \\ p stores the address of x
cout << *p << endl; \\ displays x
*p = 55; \\ overwrites x with 55
•Previous example
*p = 24;
Summarizing
•The content of p points only to a memory location of type int.
•&p, p, and *p all have different meanings.
•&p means the address of p—that is, 1200.
•p means the content of p, which is 1800, after the statement p = &num; executes.
•*p means the content of the memory location to which p points.
•Note that after the statement p = &num; executes, the value of *p is 78; after the
statement *p = 24; executes, the value of *p is 24
Pointers and Allocations
After declaring a pointer:
int *ptr1;
ptr1 doesn’t actually point to anything yet.
So its address is NULL.

. We can either:
make it point to something that already exists,
ptr1 = &C
or ptr = NULL

allocate room in memory for something new that it


will point to… (dynamic memory will discuss
later)
Memory
Pointers and Allocations
Pointing to something that already exists:
int *ptr, var1, var2;
var1 = 5;
ptr = &var1;
var2 = *ptr;
var1 and var2 have room implicitly allocated for them.

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.

What does the following code do?


We can’t store anything in the pointer (ptr) unless ptr contains
some address.

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

For example the address of (element 4th) can be 996 Element 8

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.

An integer (can be constant) may be added to or subtracted from a pointer.

Pointer variables may be subtracted from one another.

Pointer variables can be used in comparisons, but usually only in a


comparison to pointer variables or NULL.
Arithmetic on Pointers
When an integer (n) is added to or subtracted from a
pointer (ptr)
The new pointer value (ptr) is changed by the ptr
address plus (+) n multiple (*) the (bytes of ptr data
type).
ptr + n * (bytes of ptr data type)
Example
int *ptr;
ptr = 1000;
ptr = ptr + 2;
// ptr address is now changed to 1000 + 2*4 (because
integer consumes 4 bytes) New address is now 1008
Arithmetic on Pointers
Example (2)
char *ptr;
ptr = 1000; Address data

ptr++; 1000 + 1*1 = 1001 1000


1002 num1
1004
Example (3)
1006
float *ptr;
1008
ptr = 1000;
ptr+=3; 1000 + 3*4 = 1012

Example (4) Memory width is 2 bytes


int *ptr;
int num1 = 0;
ptr = &num1;
ptr++; 1002 + 4 = 1006
Arithmetic on Pointers
Example (5) Address data
void main (void)
1000
{
int *pointer1, *pointer2; 1002 num1 = 93
int num1 = 93; 1004
pointer1 = &num1; //address of num1
pointer2 = pointer1; // pointer1 address is 1006 pointer1 = 1002
assign to pointer2
1008 pointer2 = 1002

}
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;
}

Answer: Compile error


Problem 5 Find Output
int main()
{
    int num[5];
    int* p;
    p = num;
    *p = 10;
    p++;
    *p = 20;
    p = &num[2];
    *p = 30;
Answer: 10, 20, 30, 40, 50,
    p = num + 3;
    *p = 40;
    p = num;
    *(p + 4) = 50;
    for (int i = 0; i < 5; i++)
        cout << num[i] << ", ";
    return 0;
}

You might also like