lec 21 Pointers in C
lec 21 Pointers in C
Changes made inside the function are actually reflected in actual parameters of
caller
When we call a function by passing the addresses of actual parameters then this
way of calling the function is known as call by reference.
Pointers are those variables that can store address of some other variable
2
Return multiple values from a function
Using pointers
3
Example of Function call by Reference
*ptr1=20
*ptr2=10
6
Example:2 Increment the num by 1
#include<stdio.h>
void try_to_change(int *, int *);
int main()
{
int x = 10, y = 20; Expected output
printf("Initial value of x = %d\n", x);
printf("Initial value of y = %d\n", y);
printf("\nCalling the function\n");
try_to_change(&x, &y);
printf("\nValues after function call\n\n");
printf("Final value of x = %d\n", x);
printf("Final value of y = %d\n", y);
// signal to operating system everything works fine
return 0;}
void try_to_change(int *x, int *y)
{ (*x)++;
(*y)++;
printf("\nValue of x (inside function) = %d\n", *x);
7
printf("Value of y (inside function) = %d\n", *y);}
Example 3: Function Call by Reference – Swapping numbers
• The values of the variables have been changed after calling the swapnum() function
• The swap happened on the addresses of the variables num1 and num2.
8
Example 4: Function call by Reference
10 return 0;
}
Example 2: Define a function that can return quotient and remainder after
dividing two numbers from one single function.
#include<stdio.h>
void div(int a, int b, int *quotient, int *remainder) {
*quotient = a / b;
*remainder = a % b;
}
main() {
int a = 76, b = 10;
int q, r;
div(a, b, &q, &r);
printf("Quotient is: %d\nRemainder is: %d\n", q, r);
}
Output
Quotient is: 7
Remainder is: 6
11
More Examples
13
14
15
16
17
Difference between call by value and call by reference
18
Q1: Write a C Program which take elements store in an
array. Print the elements stored in the array using
pointers.
#include <stdio.h>
int main(void)
{
int scores[] = {1500, 1700, 2250};
int i = 0;
int *ptr = NULL;
ptr = &scores;
for(i=0;i<3;i++) {
printf("Value stored at scores[%d] = %d\n", i, *ptr);
/* increment */
ptr++;
}
}
Q2: Consider a scenario where you need to find out the
average marks obtained by a class of 10 students stored in an
array . The average marks should be printed using pointers?
#include <stdio.h>
int main(void)
{
int scores[] = {1500, 1700, 2250};
int i = 0,sum=0;
float avg;
int *ptr = NULL;
ptr = &scores;
for(i=0;i<3;i++) {
sum+=*ptr;
ptr++;
}
avg=sum/i;
printf("The average marks is = %0.2f\n", avg);
}
Q3: write a C program to Swap two numbers using
Pointer?
#include <stdio.h>
void swap(int *a, int *b);
int main()
{
int m = 10, n = 20;
printf("m = %d\n", m);
printf("n = %d\n\n", n);
swap(&m, &n);//passing address of m and n to the
swap function
printf("After Swapping:\n\n");
printf("m = %d\n", m);
printf("n = %d", n);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Q4: write a function that can return quotient and remainder
after dividing two numbers from one single function.
#include <stdio.h>
void div(int a,int b,int *quotient, int *remainder)
{
*quotient=a/b;
*remainder=a%b;
}
void main()
{
int a=76,b=10;
int c,d;
div(a,b,&c,&d);
printf("quotient=%d",c);
printf("\nremainder=%d",d);
}
Q6: write a function that receives 5 integers and returns the sum,
average and standard deviation of these numbers
from one single function and printed through main( ).
#include<stdio.h>
#include<math.h> //for sqrt function
void func(int a, int b, int c, int d, int e,int *summ,float *avg, float *std_dev);
int main()
{
int a,b,c,d,e,sum;
float avg,std_dev;
printf("Enter 1st number: ");
scanf("%d", &a);
printf("Enter 2nd number: ");
scanf("%d", &b);
printf("Enter 3rd number: ");
scanf("%d", &c);
printf("Enter 4th number: ");
scanf("%d", &d);
printf("Enter 5th number: ");
scanf("%d", &e);
func(a,b,c,d,e,&sum,&avg,&std_dev);
printf("The Sum is: %d\n", sum);
printf("The mean is: %0.2f\n", avg);
printf("The Standard Deviation is: %0.2f", std_dev);
return 0;
}
void func(int a, int b, int c, int d, int e,int *summ,float *avg, float *std_dev)
{
int p;
*summ = a+b+c+d+e;
*avg = *summ/5;
//standard deviation
p= ((a-*avg)*(a-*avg))+((b-*avg)*(b-*avg))+((c-*avg)*(c-*avg))+((d-
*avg)*(d-*avg))+((e-*avg)*(e-*avg));
*std_dev= sqrt(p/5);
}
Q5: write a function that can return the perimeter of
rectangle, circumference of a circle, area of rectangle
and circle from one single function and printed through
main( ). while the length, breadth and radius are input
through the keyboard.
#include <stdio.h>
int main( )
{
int r,l,w,AoR,PoR ;
float AoC,CoC;
printf ( "\nEnter radius of a circle:" ) ;
scanf ( "%d", &r ) ;
printf ( "\nEnter length of rectangle:" ) ;
scanf ( "%d", &l) ;
printf ( "\nEnter width of rectangle:" ) ;
scanf ( "%d", &w) ;
area_circum_peri ( r,l,w, &AoC, &CoC,&AoR,&PoR ) ;
printf ( "Area of circle = %0.2f", AoC ) ;
printf ( "\nCircumference of circle = %0.2f", CoC) ;
printf ( "\nArea of Rectangle = %d", AoR ) ;
printf ( "\nPerimeter of Rectangle = %d", PoR) ;
return 0;
}
void area_circum_peri( int r,int l,int w,float *aoc,float *coc,int *aor,int *por )
{
*aoc = 3.14 * r * r ;
*coc = 2 * 3.14 * r ;
*aor=l*w;
*por=2*(l+w);
}
Call by Value: Example area_circle( int r) // func definition
{
#include <stdio.h> float aoc;
int main( ) aoc = 3.14 * r * r ;
{ return aoc;
int r,l,w,AoR,PoR ; }
float AoC,CoC; circum_circle( int r) // func definition
printf ( "\nEnter radius of a circle:" ) ; {
scanf ( "%d", &r ) ; float coc;
printf ( "\nEnter length of rectangle:" ) ; coc = 2 * 3.14 * r ;
scanf ( "%d", &l) ; return coc;
printf ( "\nEnter width of rectangle:" ) ; }
scanf ( "%d", &w) ; area_rect( int l, int w) // func definition
AoC=area_circle(r) ; // func calling {
CoC=circum_circle(r) ; // func calling int aor;
AoR=area_rect(l,w) ; // func calling aor = l*w;
PoR=peri_rect(l,w) ; // func calling return aor;
printf ( "Area of circle = %0.2f", AoC ) ; }
printf ( "\nCircumference of circle = %0.2f", CoC) ; peri_rect( int l, int w) // func definition
printf ( "\nArea of Rectangle = %d", AoR ) ; {
printf ( "\nPerimeter of Rectangle = %d", PoR) ; int por;
return 0; por = 2*(l+w);
} return por;
}
34
Call by Reference: Example
#include <stdio.h>
int main( )
{
int r,l,w,AoR,PoR ;
float AoC,CoC;
printf ( "\nEnter radius of a circle:" ) ;
scanf ( "%d", &r ) ;
printf ( "\nEnter length of rectangle:" ) ;
scanf ( "%d", &l) ;
printf ( "\nEnter width of rectangle:" ) ;
scanf ( "%d", &w) ;
area_circum_peri ( r,l,w, &AoC, &CoC,&AoR,&PoR ) ;
printf ( "Area of circle = %0.2f", AoC ) ;
printf ( "\nCircumference of circle = %0.2f", CoC) ;
printf ( "\nArea of Rectangle = %d", AoR ) ;
printf ( "\nPerimeter of Rectangle = %d", PoR) ;
return 0;
}
area_circum_peri( int r,int l,int w,float *aoc,float *coc,int *aor,int *por )
{
*aoc = 3.14 * r * r ;
*coc = 2 * 3.14 * r ;
*aor=l*w;
35 *por=2*(l+w); }
36