Pointers in C++
Pointers in C++
Memory
Value at location
Location name i 3 Location number
65520 or address
Address – a class room example
1 2 3 4
4
(5, 2)
5
A program to print memory address
Main()
{
int i=4;
cout<<“Adress of i = ”<<&i;
cout<<“Value of i = ”<<i;
}
• Output of program
• Address of i = 65520
• Value of i = 3
& used in this statement is ‘address of’ operator
Cont.
Memory
Value at location
Location name f 4.5 Location number
70020 or address
ch a
85690
* operator
• The pointer operator available in C++ is *, called ‘value at address’
operator
• It gives the value stored at a particular address
• The ‘value at address’ operator is also called ‘indirection’ operator
Example No.1
int main()
{
int *ptr, x;
x = 10;
ptr = &x;
cout << "The value of x:\t\t" << x << endl;
cout << "The value of *ptr:\t" <<*ptr << endl;;
cout << "The value of ptr:\t" <<ptr << endl;;
cout << "The value of &x:\t" <<&x << endl;;
}
Problem Solving Using C – CSC1071
Example No.1 (Solution)
The value of x: 10
The value of *ptr: 10
The value of ptr: 0x22ff40
The value of &x: 0x22ff40
i 3 Program output
85065 Address of i = 85065
Value of i = 3
* (&i) = *(85065) = 3 Value of i = 3
Cont.
• We can store the address of variable i in a variable say p, i.e.
p = &i;
• p is not an ordinary variable
• It is a special variable that contains address of other variable
• Compiler also provide a space in memory for p like it do for other
variables
Cont.
Memory
i 3 p = &i;
85065
p = 85065
p 85065
76950
85065
85065
76950
85065
3
3
3
*(&i) =*(85065) = 3
*p 85065 i 3
*p = *(85065) = 3
76950 85065
Points to remember
• Pointers are variables that contain a memory address as their value
• In other words, a pointer variable contains a memory address that
points to another variable
• * operator means “value at address”
• & operator means “address of”
Dereferencing a pointer
• When you access the value *p 85065 i 3
that is present at the address 76950 85065
hold by a pointer
*p = *p +10;
*p = *(85065) +10
*p = 3 + 10
Pointer is *(85065) = 13
Deferenced
*p 85065 i 13
76950 85065
Type of pointer
int *alpha ;
char *ch ;
float *s ;
• alpha, ch and s are declared as pointer variables, i.e. variables capable
of holding addresses
• Addresses are always whole numbers, therefore pointers always
contain whole numbers.
Cont.
• Declaration float *s does not mean that s is going to contain a
floating-point value
• It means s is going to contain the address of a floating-point value
• char *ch means that ch is going to contain the address of a char value
Pointer Arithmetic
• A pointer can be assigned to the other pointer if both are of
same type
int main()
{
int a = 10, *b, *c;
b = &a; 10 10
c = b;
cout << *b << "\t" << *c;
}
change_value()
x 20
10 y 40
20
3205 4350
Cout<<x,y;
Program Output
a = 10 b = 20
Go to program x = 20 y = 40
a = 10 b = 20
Press any key to continue …
Example program 1 – function call by address
Memory
change_value(6505, 7505); main()
a 20
10 b 40
20
6504 7505
change_value()
*x 6504 *y 7505
3205 4350
Program Output
a = 10 b = 20 *x =*(6504) = 10
Go to program x = 20 y = 40
a = 20 b = 40
Press any key to continue …
Example program 2-function call by value
Go to program
Example program 2-function call by address
Go to program
Return value of a function
Write a program
Conclusion
• If we want that the value of an actual argument should not get
changed in the function being called, pass the actual argument by
value.
•
Go to program
Example program – factorial using recursion
Cout<<“Enter any number”;
Cin>>a
Cout<<“Factorial is = ”<<fact;
Go to program
Cont.
Cont.
rec ( 3 ) rec ( 2 )
main() { {
{ int f ; int f ;
…. if ( 3 == 1 ) false if ( 2 == 1 ) false
…. return ( 1 ) ; return ( 1 ) ;
fact = rec(3); else else
printf ( "%d ", fact); f = 3 * rec ( 3 - 1 ) ; f = 2 * rec ( 2 - 1 ) ;
} return ( f ) ; return ( f ) ;
} }
rec ( 1 )
f = 3 * 2; f = 2 * 1; {
f=6 f=2 int f ;
if ( 1 == 1 ) true
return ( 1 ) ;
fact = 6
else
f = 2 * rec ( 2 - 1 ) ;
return ( f ) ;
}
Example program 2
• Write a definition of a function that adds n integers using recursion
and then return the sum. Prototype of the function is below
• int sum_number(int);
Write a program
Array
• Offers a simple way of grouping like variables for easy access
• It is a group of elements having same data type
• An array is a collective name given to a group of ‘similar quantities’
• Arrays in C share a few common attributes
• Variables in an array share the same name
• Variables in an array share the same data type
• Individual variables in an array are called elements
• Elements in an array are accessed with an index number
Cont.
Cout<<“x= ”<<x;
0 1 2 3 4 5 6 7 8 9
500 504 508 512 516 520 524 528 532 530
Memory address of array elements
How to access array elements
int x; int marks[10];
x= 2; marks[0] = 2;
Cout<<x; marks[1] = 3;
Cout<<x; Cout<<marks[2])
Cout<<“marks [2] = ”<< marks[2])
x 43
2 Output
695 x = 43 Output
marks [2] = 16
0 1 2 3 4 5 6 7 8 9
2 3 16
500 504 508 512 516 520 524 528 532 530
Points to remember
• Array is a collection of elements having same data type
• Memory allocate to array elements are continuous
• Array size must be mentioned in array declaration ( int marks [10]; )
• Array index always starts with 0
• In 10 elements array, index of first elements is 0 and index of last
element is 9
• Array element can be access using array index
A Simple Program Using Array
• Write a program that take 10 integer from user and then display those integers
Write a program
Marks program
Cout<<“Average is = ”<<avg<<endl;
Go to program
Array
• Offers a simple way of grouping like variables for easy access
• It is a group of elements having same data type
• An array is a collective name given to a group of ‘similar quantities’
• Arrays in C share a few common attributes
• Variables in an array share the same name
• Variables in an array share the same data type
• Individual variables in an array are called elements
• Elements in an array are accessed with an index number
Cont.
Cout<<“x= ”<<x;
0 1 2 3 4 5 6 7 8 9
500 504 508 512 516 520 524 528 532 530
Memory address of array elements
How to access array elements
int x; int marks[10];
x= 2; marks[0] = 2;
Cout<<x; marks[1] = 3;
Cout<<x; Cout<<marks[2])
Cout<<“marks [2] = ”<< marks[2])
x 43
2 Output
695 x = 43 Output
marks [2] = 16
0 1 2 3 4 5 6 7 8 9
2 3 16
500 504 508 512 516 520 524 528 532 530
Points to remember
• Array is a collection of elements having same data type
• Memory allocate to array elements are continuous
• Array size must be mentioned in array declaration ( int marks [10]; )
• Array index always starts with 0
• In 10 elements array, index of first elements is 0 and index of last
element is 9
• Array element can be access using array index
A Simple Program Using Array
• Write a program that take 10 integer from user and then display those integers
Write a program
Marks program
Cout<<“Average is = ”<<avg<<endl;
Go to program
Marks program
Go to program
Array initialization
Bounds Checking
• In C++ there is no check to see if the subscript used for an array exceeds the size of
the array
• Data entered with a subscript exceeding the array size will simply be placed in
memory outside the array
0 1 2 3 4 5 6 7 8 9
500 504 508 512 516 520 524 528 532 530
• To see to it that we do not reach beyond the array size is entirely the programmer’s
botheration and not the compiler’s
Passing Array Elements to a Function
• Array elements can be passed to a function by calling the function by value, or
by address
• In the call by value we pass values of array elements to the function
• In the call by reference we pass addresses of array elements to the function
Value of array element pass to a function
0 1 2 3 4 5 6
55 65 75 56 78 78 90
500 504 508 512 516 520 524
ii ==406
2
3
1
5
7
display(marks[6])
display(marks[5])
display(marks[2])
display(marks[3])
display(marks[4])
display(marks[o])
display(marks[1])
display(90)
display(78)
display(75)
display(56)
Program output display(55)
display(65)
Cout<<m;
55
65
75
56
78
78
90
Address of array element pass to a function
0 1 2 3 4 5 6
55 65 75 56 78 78 90
500 504 508 512 516 520 524
ii ==406
2
3
1
5
7
display(&marks[4])
display(&marks[2])
display(&marks[3])
display(&marks[1])
display(&marks[5])
display(&marks[6])
display(&marks[o])
Program output display(508)
display(516)
display(512)
display(504)
display(520)
display(524)
display(500)
55
n
Cout<<* ; 65 *n
*n 524
512
520
516
504
508
500
75 *(512)
*(520)
*(524)
*(516)
*(504)
*(500) 56
*(508) = 90
78
65
75
55
56
78
78
90
Pointer arithmetic
• Addition of a number to a pointer
*j 500
504 *k 516
0 1 2 3 4 5
*j 10
10 20 30 40 50 60
j 500
500 504 508 512 516 520
Cont.
• Subtraction of a number from a pointer
*k 504 *j 516
520
0 1 2 3 4 5
10 20 30 40 50 60
500 504 508 512 516 520
Cont.
• Subtraction of one pointer from another
• One pointer variable can be subtracted from another provided
both variables point to elements of the same array
• The resulting value indicates the number of bytes separating the
corresponding array elements
Cont. Go to program
*i 504 *j 520
0 1 2 3 4 5 6
10 20 30 45 67 56 74
500 504 508 512 516 520 524
Cont.
• Comparison of two pointer variables
• Pointer variables can be compared provided both variables point to elements of the
same data type
Go to program
Caution
• Following would not work
• Addition of two pointers
• Multiplication of a pointer with a constant
• Division of a pointer with a constant
Points to remember
• Array elements are always stored in contiguous memory locations.
• A pointer when incremented always points to an immediately next
location of its type
• A pointer when decremented always points to the immediate
previous location of its type
• Comparison between pointers can test for either equality or
inequality
Display array value using pointer
• Method of accessing array elements by using subscripted variables is
already known to us
• Now let see how we can access the array elements using a pointer.
Write a program