SlideShare a Scribd company logo
Double Pointer
Call by Value and Call by reference
Pointers taking Multiple References
Content to be covered
• Double Pointer Concept
• Call by value
• Call by reference
• Example program to understand
Double Pointer Concept
• When a pointer start keeping address of another pointer.
• This is called either Double Pointer or Pointer to Pointer.
• To fetch value from a double pointer we have to use
multiple reference by using ** or ***.
• To obtain address of a pointer we can use & operator
itself.
Now check the following declarations:
int *a, **b, ***c;
And normal integer variable
int d=10;
The assignments
a=&d;
b=&a;
c=&b;
In above statements a is pointing to d’s address, b is pointing to a’s address
and c is pointing to b’s address.
*a gives 10
**b gives 10
***c gives 10
So a will be called single pointer, b, c is called Double Pointer.
10 1000 2000 3000
1000 2000 3000 4000
d a b c
Call by Value & Call by Reference
• Call by value: This is normal calling mechanism which
we have studied in functions in which the value of actual
parameters gets copied into formal parameters.
• Call by Reference: In this calling mechanism reference
(address) of actual parameters are copied to formal
parameters which are actually pointers of same data
type so that they are capable to keep addresses.
Difference between Call by Value & Call by Reference
• Difference between above two: The major difference between above two
is; in call by value, values are copied to local variables therefore whatever
changes you are making in them they are temporary till the execution of
that function as you come out of function they disappears while in call by
reference they remains permanent because addresses are copied to the
formal parameters and changes are made to them by using pointers at
actual memory location original data gets changed.
• Let’s have a program to be clear about call by value and call by reference.
#include<stdio.h>
#include<conio.h>
void change1 (int, int);
void change2 (int *, int *);
void change1 (int a, int b)
{
a=0;
b=0;
printf(“%d%d”,a,b);
}
void change2 ( int *a, int *b)
{
*a=0;
*b=0;
printf(“%d%d”,*a, *b);
}
Changes are performed in memory of a and b of change1() which are
treated as local variables so changes are temporary which will disappear as
execution of function definition will be over i.e. changes in a and b of this
function will not affect the a and b inside main() function.
Changes are performed in memory of a and b of change2() which are
treated as local variables but they are pointers and changing the values
on addresses of a and b of main() function so changes are permanent
which will remain in memory even after execution of function definition
i.e. changes in a and b of this function will affect the a and b inside
main() function.
void main()
{
int a,b;
a=10;
b=20;
printf(“%d%d”,a,b);
change1(a,b);
printf(“%d%d”, a,b);
change2(&a, &b);
printf(“%d%d”,a,b);
getch();
}
Output:
10 20
0 0
10 20
0 0
0 0
Passing values of actual parameters to
formal parameters therefore mechanism
is called “call by value”.
Passing address of actual parameters which
will be further copied to formal parameters
(pointer variables) in function definition so
it is call by reference (call by address).

More Related Content

PPT
First Look at Pointers
PPTX
Function Pointer in C
PPTX
Types of function call
PPTX
parameter passing in c#
PPTX
Call by value or call by reference in C++
PPTX
Call by value
PPTX
PARAMETER PASSING MECHANISMS
PPTX
Pointer to function 1
First Look at Pointers
Function Pointer in C
Types of function call
parameter passing in c#
Call by value or call by reference in C++
Call by value
PARAMETER PASSING MECHANISMS
Pointer to function 1

What's hot (20)

PPTX
Function Pointer
PPTX
Functions (Computer programming and utilization)
PDF
Pointers in C language
PPTX
Abdullah alotaibi functions
PPTX
Parameter passing to_functions_in_c
PPT
CPP Language Basics - Reference
PPTX
Pointer to function 2
PPTX
Presentation on function
PPTX
Pointer basics
PPT
Function overloading(C++)
PPT
C++ Function
ODP
Function
PPT
Function in C Language
PPT
Functions in C++
PPTX
Function in c
PPT
User Defined Functions in C
PPTX
Functionincprogram
PPT
Prsentation on functions
PPT
16717 functions in C++
 
PPTX
Method parameters in c#
Function Pointer
Functions (Computer programming and utilization)
Pointers in C language
Abdullah alotaibi functions
Parameter passing to_functions_in_c
CPP Language Basics - Reference
Pointer to function 2
Presentation on function
Pointer basics
Function overloading(C++)
C++ Function
Function
Function in C Language
Functions in C++
Function in c
User Defined Functions in C
Functionincprogram
Prsentation on functions
16717 functions in C++
 
Method parameters in c#
Ad

Similar to Pointers lesson 5 (double pointer, call by value, call_by_reference) (20)

PPTX
2-Concept of Pointers in c programming.pptx
PPTX
Function (rule in programming)
PPTX
CH.4FUNCTIONS IN C (1).pptx
PDF
Functions in C++.pdf
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
PPTX
FYBSC(CS)_UNIT-1_Pointers in C.pptx
PPT
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
PPT
pointer, structure ,union and intro to file handling
PPT
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
PPT
pointer, structure ,union and intro to file handling
PPT
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
PPT
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
PPTX
Pointer.pptx
PPTX
Pointer in C++
PPTX
unit_2 (1).pptx
PPTX
FUNCTIONS1.pptx
PDF
PSPC--UNIT-5.pdf
PPT
Pointers operation day2
PPTX
C++ FUNCTIONS-1.pptx
PDF
Pointers.pdf
2-Concept of Pointers in c programming.pptx
Function (rule in programming)
CH.4FUNCTIONS IN C (1).pptx
Functions in C++.pdf
FUNCTIONS, CLASSES AND OBJECTS.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Pointer.pptx
Pointer in C++
unit_2 (1).pptx
FUNCTIONS1.pptx
PSPC--UNIT-5.pdf
Pointers operation day2
C++ FUNCTIONS-1.pptx
Pointers.pdf
Ad

Recently uploaded (20)

PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Construction Project Organization Group 2.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
PPT on Performance Review to get promotions
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
composite construction of structures.pdf
PPTX
web development for engineering and engineering
R24 SURVEYING LAB MANUAL for civil enggi
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Construction Project Organization Group 2.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Foundation to blockchain - A guide to Blockchain Tech
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Geodesy 1.pptx...............................................
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
CH1 Production IntroductoryConcepts.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPT on Performance Review to get promotions
OOP with Java - Java Introduction (Basics)
bas. eng. economics group 4 presentation 1.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
composite construction of structures.pdf
web development for engineering and engineering

Pointers lesson 5 (double pointer, call by value, call_by_reference)

  • 1. Double Pointer Call by Value and Call by reference Pointers taking Multiple References
  • 2. Content to be covered • Double Pointer Concept • Call by value • Call by reference • Example program to understand
  • 3. Double Pointer Concept • When a pointer start keeping address of another pointer. • This is called either Double Pointer or Pointer to Pointer. • To fetch value from a double pointer we have to use multiple reference by using ** or ***. • To obtain address of a pointer we can use & operator itself.
  • 4. Now check the following declarations: int *a, **b, ***c; And normal integer variable int d=10; The assignments a=&d; b=&a; c=&b; In above statements a is pointing to d’s address, b is pointing to a’s address and c is pointing to b’s address. *a gives 10 **b gives 10 ***c gives 10 So a will be called single pointer, b, c is called Double Pointer. 10 1000 2000 3000 1000 2000 3000 4000 d a b c
  • 5. Call by Value & Call by Reference • Call by value: This is normal calling mechanism which we have studied in functions in which the value of actual parameters gets copied into formal parameters. • Call by Reference: In this calling mechanism reference (address) of actual parameters are copied to formal parameters which are actually pointers of same data type so that they are capable to keep addresses.
  • 6. Difference between Call by Value & Call by Reference • Difference between above two: The major difference between above two is; in call by value, values are copied to local variables therefore whatever changes you are making in them they are temporary till the execution of that function as you come out of function they disappears while in call by reference they remains permanent because addresses are copied to the formal parameters and changes are made to them by using pointers at actual memory location original data gets changed. • Let’s have a program to be clear about call by value and call by reference.
  • 7. #include<stdio.h> #include<conio.h> void change1 (int, int); void change2 (int *, int *); void change1 (int a, int b) { a=0; b=0; printf(“%d%d”,a,b); } void change2 ( int *a, int *b) { *a=0; *b=0; printf(“%d%d”,*a, *b); } Changes are performed in memory of a and b of change1() which are treated as local variables so changes are temporary which will disappear as execution of function definition will be over i.e. changes in a and b of this function will not affect the a and b inside main() function. Changes are performed in memory of a and b of change2() which are treated as local variables but they are pointers and changing the values on addresses of a and b of main() function so changes are permanent which will remain in memory even after execution of function definition i.e. changes in a and b of this function will affect the a and b inside main() function.
  • 8. void main() { int a,b; a=10; b=20; printf(“%d%d”,a,b); change1(a,b); printf(“%d%d”, a,b); change2(&a, &b); printf(“%d%d”,a,b); getch(); } Output: 10 20 0 0 10 20 0 0 0 0 Passing values of actual parameters to formal parameters therefore mechanism is called “call by value”. Passing address of actual parameters which will be further copied to formal parameters (pointer variables) in function definition so it is call by reference (call by address).