0% found this document useful (0 votes)
18 views64 pages

Pointers in C++

1. The document discusses pointers in C++, including pointer fundamentals, the & and * operators, pointer declaration and type, and how pointers store memory addresses. 2. It provides examples of how pointers can store and dereference memory addresses, and how arguments can be passed by value or by reference to functions using pointers. 3. Recursive functions are discussed, with an example provided of a recursive function to calculate the factorial of a number by calling itself.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views64 pages

Pointers in C++

1. The document discusses pointers in C++, including pointer fundamentals, the & and * operators, pointer declaration and type, and how pointers store memory addresses. 2. It provides examples of how pointers can store and dereference memory addresses, and how arguments can be passed by value or by reference to functions using pointers. 3. Recursive functions are discussed, with an example provided of a recursive function to calculate the factorial of a number by calling itself.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 64

Programming Fundamental

Today’s lecture outline


• Pointer fundamental
• & operator
• * operator
• Pointer declaration
• Pointer type
Simple variable declaration
• Consider the declaration,

• This declaration tells the C++ compiler to:


• Reserve space in memory to hold the integer value
• Associate the name i with this memory location
• Store the value 3 at this location

Memory
Value at location
Location name i 3 Location number
65520 or address
Address – a class room example
1 2 3 4

1 Address of this chair


2 2nd row and 3rd column
(2, 3)
3

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

Problem Solving Using C – CSC1071


Example program
Go to program

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

i’s value is 3 and p’s value is i’s address


Pointer declaration
Go to program

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

Problem Solving Using C – CSC1071


Function Calls
• Arguments can generally be passed to functions in one of the two
ways:
• sending the values of the arguments
• sending the addresses of the arguments
Example program 1 – function call by value
Memory
main()
Int a = 10, b = 20;
Cout<<a<<b; a 10 b 20
Change_value(a,b); 6504 7505
Cout<<a,b;

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

Cout<<“Enter any number ”;


Cin>>a;

Cout<<“Squar of “<<a<<“is ”<<b;


Points to remember
• Passing arguments by value is not the most efficient means for
programming in C++
• When arguments are passed by value, the called function is unable to
modify the original contents of the incoming parameters
• When arguments are passed by address, the called function is able to
modify the original contents of the incoming parameters
• A function only return a single value
Example program
• Write a program in which a user input radius of a
circle. The program calculates the area and the
perimeter of the circle using a single 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.

• If we want that the value of an actual argument should get changed in


the function being called, pass the actual argument by reference.

• If a function is to be made to return more than one value at a time


then return these values indirectly by using a call by reference.
Recursive function
• In C++, it is possible for the functions to call themselves
• A function is called ‘recursive’ if a statement within the body of a
function calls the same function
Example program - factorial

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;

• Ordinary variables are capable of holding only one value at a time


• There are situations in which we would want to store more than one value at
a time in a single variable
Cont.
• For example, suppose we want to arrange the percentage marks obtained by
100 students in ascending order
• In such a case we have two options to store these marks in memory:
• Declare 100 variables to store percentage marks obtained by 100 different students, i.e. each
variable containing marks of single student
int m1, m2, m3 ……… m100;
• Declare one variable (called array or subscripted variable) capable of storing or holding all the
hundred values
Array declaration
int marks[10];

type Array name size


Like any other variable, arrays occupy memory space
marks [10] Index of elements in array

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<<“Enter Marks ”<<endl;


Cin>>marks[i];

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;

• Ordinary variables are capable of holding only one value at a time


• There are situations in which we would want to store more than one value at
a time in a single variable
• For Cont.
example, suppose we want to arrange the percentage marks obtained by
100 students in ascending order
• In such a case we have two options to store these marks in memory:
• Declare 100 variables to store percentage marks obtained by 100 different students, i.e. each variable
containing marks of single student
int m1, m2, m3 ……… m100;
• Declare one variable (called array or subscripted variable) capable of storing or holding all
the hundred values
Array declaration
int marks[10];

type Array name size


Like any other variable, arrays occupy memory space
marks [10] Index of elements in array

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<<“Enter Marks ”<<endl;


Cin>>marks[i];

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

Cout<<j - i<<*j - *i;

*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

You might also like