Powerpoint Templates Page 1
MODULE 2
Pointers
pointers to array
Pointer and functions
Pointer to strings
Array of pointers
pointers to pointers
Dynamic memory allocation
Powerpoint Templates Page 2
Introduction
• Pointers are variables that hold address of another variable of
same data type.
• Benefits:
Pointers are more efficient in handling Array and Structure.
Pointer allows reference to function and thereby helps in
passing of function as arguments to other function.
It reduces length and the program execution time.
It allows C to support dynamic memory management.
Powerpoint Templates Page 3
Concept of Pointer
Powerpoint Templates Page 4
Operators used in Pointers
Dereferencing Address
(Value of)
&(Address of)
Powerpoint Templates Page 5
Int i=3;
Address of ‘i’ Value of ‘i’
‘&i’ variable
i
‘*i’
3
(Value of i)
X1000 x1004 x1008 x100c x1010 x1014
Address of i
The value ‘3’Powerpoint
is saved in the memory location ‘x100c’
Templates Page 6
Declaring a Pointer
• General syntax of pointer declaration is
data_type *pointer_name;
• Rule:
Data type of pointer must be same as the variable, which the
pointer is pointing.
• Note:
void data type pointer works with all data types, but its not used
oftenly.
Powerpoint Templates Page 7
Initialization of Pointer variable
• Pointer initialization is the process of assigning address of a
variable to pointer variable.
Pointer variable contains address of variable of same data type.
In C language, address operator & is used to determine the address of a
variable.
int a = 10;
int *ptr; //pointer declaration
ptr = &a; // pointer initialization
• Note:
Pointer variable always point to same type of data.
Powerpoint Templates Page 8
• There are two unary operations to consider.
• The * operator: If ptra is a pointer variable, then∗ ptra
gives you the content of the location pointed to by ptr.
• The & operator: If v is a variable, then &v is the address of the
variable.
Powerpoint Templates Page 9
Dereferencing of Pointer
• Once a pointer has been assigned the address of a variable, to access
the value of variable
The pointer is dereferenced, using the indirection operation *
int a, *p;
a = 10;
p = &a;
printf(“%d”, *p);
printf(“%d”, *&a);
printf(“%u”, &a);
printf(“%u”, p);
printf(“%u”, &p);
Powerpoint Templates Page 10
Dereferencing Operator *
• We can access to the value stored in the variable pointed to by using the dereferencing
operator (*),
Memory address: 1020 1024 1032
… 88 100 … 1024 …
a p
int a = 100;
int *p = &a; Result is:
cout << a << endl; 100
cout << &a << endl; 1024
cout << p << " " << *p << endl; 1024 100
cout << &p << endl; 1032
Powerpoint Templates Page 11
Lets Take an Example and See how pointers work
#include<stdio.h>
Void main()
{
Int i=3;
Int *j;
j=&i;
Printf(“i=%d”i);
Printf(“*j=%d”*j);
} Powerpoint Templates Page 12
Int i=3; Create an integer variable ‘i’ and initialize it to 3
Int *j; Create a pointer variable ‘j’- create value of ‘j’
j = &i; Initialize the pointer value of ‘j’ to the address of ‘i’
variables
Int *j Int i
M
e 3
m x100c
or
y
X1000 x1004 Powerpoint Templates
x1008 x100c x1010 x1014
Page 13
Output screen
Printf(“i=%d” i);
Printf(“*j=%d” *j);
i=3
We know j=&i *j=3
So *j=*(&i) value of (address of i)
(i.e.) value in address (x100c)
Int *j Int i
M
e 33
m x100c
or
y
X1000 x1004 x1008
Powerpoint Templates x100c x1010 x1014
Page 14
Example : Sum of two numbers using Pointers
#include <stdio.h>
main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &first, &second);
p = &first;
q = &second;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
}
Powerpoint Templates Page 15
Predict the output of this code
Void main()
{
int num=10;
int* pnum=NULL;
pnum = #
*pnum += 20;
printf("\nNumber = %d", num);
printf("\nPointer Number = %d", *pnum);
}
Powerpoint Templates Page 16
Number = 10
Pointer Number = 30
Powerpoint Templates Page 17
Work to your Brain
int a[10] = {1,2,3,4,5,6,7,8,9,12} ,*p, *q , i;
p = &a[2];
q = &a[5];
i = *q - *p;
Printf(“The value of i is %d” i );
i = *p - *q;
Printf(“The value of i is %d” i );
a[2] = a[5] = 0;
Printf(“The value of i is %d” i );
Powerpoint Templates Page 18
The value of i is 3
The value of i is -3
The value of i is 0
Powerpoint Templates Page 19
Work to your Brain
int a[10] = { 2,3,4,5,6,7,8,9,1,0 }, *p, *q;
p = &a[2];
q = p + 3;
p = q – 1;
p+ + ;
Printf(“The value of p and q are : %d , %d” *p,*q);
Powerpoint Templates Page 20
The value of p and q are : 7 , 7
Powerpoint Templates Page 21
Pointer Arithmetic
There are four arithmetic operators that can be used on pointers:
++, --, +, and -
Powerpoint Templates Page 22
Pointer Arithmetic
Given a pointer p, p+n refers to the element that is offset from p by n
positions.
a 2 p - 1
a + 1 4 p
a + 2 6 p + 1
a + 3 8 p + 2
a + 4 22 p + 3
Powerpoint Templates Page 23
Programs
Powerpoint Templates Page 24
Programs
// This program uses a pointer to display the contents of an integer
array.
#include <iostream.h>
void main(void)
{
int set[8] = {5, 10, 15, 20, 25, 30, 35, 40};
int *nums, index;
nums = set;
cout << "The numbers in set are:\n";
for (index = 0; index < 8; index++)
{
cout << *nums << " ";
nums++;
}
Powerpoint Templates Page 25
Program continues
cout << "\nThe numbers in set backwards
are:\n";
for (index = 0; index < 8; index++)
{
nums--;
cout << *nums << " ";
}
}
• Output
The numbers in set are:
5 10 15 20 25 30 35 40 Powerpoint Templates Page 26
The numbers in set backwards are:
NULL Pointers
• assign a NULL value to a pointer variable in case you do not
have an exact address to be assigned
• done at the time of variable declaration. A pointer that is
assigned NULL is called a null pointer
#include <stdio.h>
int main () Output
{ The value of ptr is 0
int *ptr = NULL;
printf("The value of ptr Powerpoint
is : %x\n", ptr );
Templates Page 27
Pointer to Pointer
What is the output?
58 58 58
Powerpoint Templates Page 28
THANK YOU
Powerpoint Templates Page 29