Pointers, Call-By-Value, Call-By-Reference (DM)
Pointers, Call-By-Value, Call-By-Reference (DM)
POINTERS
CALL BY VALUE VERSUS CALL BY
REFERENCE
1
Basic Concept
A pointer is a variable that stores the location/address (rather than the value)
of a data item.
Within the computer memory, every stored data item occupies one or more
contiguous memory cells.
The number of memory cells required to store a data item depends on its type (char, int,
double, etc.).
2
Accessing address of a variable
If var is the name of the variable, then &var will give it's address.
#include<stdio.h>
void main()
{
int var = 13;
printf("Value of the variable var is: %d\n", var);
printf("Memory address of the variable var is: %x\n", &var);
}
Output:-
3
Pointer Declarations
1. The asterisk (*) tells that the variable pointer_name is a pointer variable.
2. pointer_name is the name of the pointer variable.
3. Pointer pointer_name points to a variable of type data_type.
4
Pointer Declarations (Contd..)
Example:
int *year;
float *interest;
Once a pointer variable has been declared, it can be made to point to a variable using an
assignment statement like:
int *p, a;
:
p = &a;
◦ This is called pointer initialization.
5
Pointer Declarations (Contd..)
Pointer variables must always point to a data item of the same type.
float a;
int *p;
: will result in erroneous output
p = &a;
Assigning an absolute address to a pointer variable is prohibited.
int *count;
:
count = 7659;
6
Accessing a Variable Through its Pointer
Once a pointer has been assigned the address of a variable, the value of the
variable can be accessed using the indirection operator (*).
int a, b;
int *p;
: Equivalent to b=a
p = &a;
b = *p;
7
Example-1
#include <stdio.h>
void main( )
{
int a, b;
int c = 5;
int *p;
Equivalent
a = 4 * (c + 5) ;
p = &c; Output:
b = 4 * (*p + 5) ; a=40 b=40
printf(“a=%d b=%d \n”, a, b) ;
}
8
Pointer Expressions
9
Pointer Expressions (Contd..)
What are not allowed?
10
Example-2
#include <stdio.h>
int main() Output
{
int *p1; 1010051596
int i=1; 1010051598
p1=&i; 1010051592
printf("%d\n",p1);
printf("%d\n",p1 + 1);
p1=p1-(i + 1);
printf("%d",p1);
return(0);
}
11
Analysis on Example-2
In reality, it is not the integer value which is added/subtracted, but rather the
scale factor times the value.
Data Type Scale Factor(Size in byte)
char 1
int 2
float 4
double 8
The size is system dependent.
12
Analysis on Example-2
p1=&i;
i
p1
1010051596 1
1010051423 1010051596
printf("%d\n",p1); 1010051596
13
Call by value vs Call by reference
14
Example-3 (Call-by-value)
#include <stdio.h>
void swap(int, int);
a and b
void main( )
do not
{
swap
int a, b;
a = 5 ; b = 20 ;
swap (a, b) ; Output
printf (“\n a = %d, b = %d”, a, b);
} a = 5, b = 20
void swap (int x, int y)
{
int t ;
t=x; x and y swap
x=y;
y=t;
}
15
Example-3 (Call-by-reference)
#include <stdio.h>
void swap(int *, int *);
void main( ) *(&a) and *(&b)
{ swap
int a, b;
a = 5 ; b = 20 ;
swap (&a, &b) ; Output
printf (“\n a = %d, b = %d”, a, b);
} a = 20, b = 5
void swap (int *x, int *y)
{
int t ; *x and *y
t = *x ; swap
*x = *y ;
*y = t ;
}
16
THANK YOU
17