0% found this document useful (0 votes)
12 views

Pointers, Call-By-Value, Call-By-Reference (DM)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Pointers, Call-By-Value, Call-By-Reference (DM)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming in C

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.).

Whenever we define a variable, the system allocates memory location(s) to


hold the value of the variable.
 Since every byte in memory has a unique address, this location will also have its own
(unique) address.

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:-

Value of the variable var is: 13


Memory address of the variable var is: ef305bac (Address is not fixed)

3
Pointer Declarations

Pointer variables must be declared before we use them.


General form:
data_type *pointer_name;
 Three things are specified in the above declaration:

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

Like other variables, pointer variables can be used in expressions.


If p1 and p2 are two pointers, the following statements are valid:

sum = *p1 + *p2 ;


prod = *p1 * *p2 ;
prod = (*p1) * (*p2) ;
*p1 = *p1 + 2;
x = *p1 / *p2 + 5 ;

9
Pointer Expressions (Contd..)
What are not allowed?

◦ Add two pointers.


p1 = p1 + p2 ;

◦ Multiply / divide a pointer in an expression.


p1 = p2 / 5 ;
p1 = p1 – p2 * 10 ;

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

printf("%d\n",p1 + 1); 1010051598 = (1010051596 + 1*2)

p1=p1-(i + 1); P1= 1010051596 – 2 * 2 = 1010051592


printf("%d",p1); 1010051592

13
Call by value vs Call by reference

Normally, arguments are passed to a function by value.


◦ The data items are copied to the function.
◦ Changes are not reflected in the calling program.
◦ Called call-by-value.

Pointers are often passed to a function as arguments.


◦ Allows data items within the calling program to be accessed by the function,
altered, and then returned to the calling program in altered form.
◦ Called call-by-reference (or by address or by location).

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

You might also like