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

notes set 5

Uploaded by

suganya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

notes set 5

Uploaded by

suganya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Fundamentals of computing

C Call by Reference: Using pointers

When a pointer is passed as an argument to a function, address of the memory location is passed
instead of the value.

This is because, pointer stores the location of the memory, and not the value.

Program to swap two number using call by reference.

/* C Program to swap two numbers using pointers and function. */


#include <stdio.h>
void swap(int *n1, int *n2);
int main()
{
int num1 = 5, num2 = 10;
// address of num1 and num2 is passed to the swap function
swap( &num1, &num2);
printf("Number1 = %d\n", num1);
printf("Number2 = %d", num2);
return 0;
}
void swap(int * n1, int * n2)
{
// pointer n1 and n2 points to the address of num1 and num2 respectively

A.Suganya AP II/ SoC/ Sastra University Page 1


Fundamentals of computing

int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}

Output

Number1 = 10

Number2 = 5

The address of memory location num1 and num2 are passed to the function swap and the
pointers *n1 and *n2 accept those values.

So, now the pointer n1 and n2 points to the address of num1 and num2 respectively.

When, the value of pointers are changed, the value in the pointed memory location also changes
correspondingly.

Hence, changes made to *n1 and *n2 are reflected in num1 and num2 in the main function.

This technique is known as Call by Reference in C programming.

#include <stdio.h>
void increment(int *var)
{
/* Although we are performing the increment on variable
* var, however the var is a pointer that holds the address
* of variable num, which means the increment is actually done
* on the address where value of num is stored.
*/
*var = *var+1;
}
int main()
{
int num=20;
/* This way of calling the function is known as call by
* reference. Instead of passing the variable num, we are

A.Suganya AP II/ SoC/ Sastra University Page 2


Fundamentals of computing

* passing the address of variable num


*/
increment(&num);
printf("Value of num is: %d", num);
return 0;
}
Output:

Value of num is: 21


#include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter Two Values: ");
scanf("%d %d",&a,&b);
c=add(&a,&b);
printf("The Sum is: %d",c);
getch();
}
add(int *x,int *y)
{
int z;
z=*x+*y;
return(z);
}

OUTPUT:

Enter Two Values: 30 20


The Sum is: 50
#include <stdio.h>

void call_by_reference(int *y) {


printf("Inside call_by_reference y = %d before adding 10.\n", *y);
(*y) += 10;
printf("Inside call_by_reference y = %d after adding 10.\n", *y);
}

int main() {
int b=10;

printf("b = %d before function call_by_reference.\n", b);


call_by_reference(&b);
printf("b = %d after function call_by_reference.\n", b);

A.Suganya AP II/ SoC/ Sastra University Page 3


Fundamentals of computing

return 0;
}

Difference between call by value and call by reference.

call by value call by reference


This method copy original value into This method copy address of
function as a arguments. arguments into function as a
arguments.
Changes made to the parameter inside Changes made to the parameter
the function have no effect on the affect the argument. Because
argument. address is used to access the actual
argument.
Actual and formal arguments will be Actual and formal arguments will
created in different memory location be created in same memory location

#include<stdio.h>
void main()
{
int A=10,B=20;
printf("\nValues before calling %d, %d",A,B);
fun(&A,&B); //Statement 1
printf("\nValues after calling %d, %d",A,B);
}
void fun(int *X,int *Y) //Statement 2
{
*X=11;
*Y=22;
}
Output :
Values before calling 10, 20
Values after calling 11, 22

A.Suganya AP II/ SoC/ Sastra University Page 4


Fundamentals of computing

A.Suganya AP II/ SoC/ Sastra University Page 5

You might also like