KEMBAR78
C Recursion, Pointers, Dynamic memory management | PDF
11Mar2022: Recursion, Pointers, Dynamic memory management
PROGRAMMING FOR
PROBLEM SOLVING (PPS)
B.Tech I Sem CST
Schedule
Date Topic
11 Mar 2022 Recursion, Pointers:
Intro,Dynam mem.mgmt
17 Mar 2022 Pointer arrays,functions
24 Mar 2022 Multidime.arrays;
Structures: Intro
25 Mar 2022 Structures: arrays,
functions; Selfref.strc
Date Topics
31 Mar 2022 Typedef,union
1 Apr 2022 Tutorial (Lab
pgms)
7 Apr 2022 Files:open,clos
e,read,write
8 Apr 2022 File error
handlng
Quiz & Internal Lab exam dates
Date Details
30 Mar 2022
(6.30 to 8.00pm)
Quiz 3 & Quiz 4
6 Apr 2022 Internal Lab exam
Today’s Topics
 Recursion
 Pointers
 Pointers, address, pointer to pointer
 Initialization of pointer arrays
 Dynamic memory management
Recursion: process of repeating the similar steps.
Recursive function: a function that calls itself.
Recursive functions types : Direct and Indirect recursive
functions
Direct recursive
A function is said to be
direct recursive if it calls
itself directly.
Ex:
int fun1 (int n)
{
if (cond) return 1;
else return (fun1(n-1));
}
Indirect Recursive
A function is said to be indirect recursive if
it calls another function and this new function
calls the first calling function again.
Ex:
int func1(int n)
{
if (cond1) return 1;
else return func2(n);
}
int func2(int n)
{
return func1(n);
}
f1
f1 f2 fn
…
Sum of ‘n’ natural
numbers
int sum(int n) {
if (n!=0) return n+sum(n-1); else return n; }
Factorial of given
number
int factorial(int n) {
if(n==1) return 1; else return n*factorial(n-1); }
Fibonacci series int fib(int n) {
if(n==0) return 0; else if(n==1) return 1;
else return(fib(n-1)+fib(n-2)); }
GCD of two no’s int gcd(int m,int n) {
if(n==0) return m; else return (gcd(n,m%n)); }
Towers of Hanoi void TOH(n,s,d,i) {
If(n==1) { Print s, d; return; }
TOH(n-1,s,i,d);
Print n,s,d
TOH(n-1,i,d,s); }
A Classical Case: Towers of Hanoi
• The towers of Hanoi problem involves moving a
number of disks (in different sizes) from one
tower (or called “peg”) to another.
– The constraint is that the larger disk can never be
placed on top of a smaller disk.
– Only one disk can be moved at each time
– Assume there are three towers available.
Towers of Hanoi
Towers of Hanoi
void TOH(int n, char s, char d, char i)
{
if (n == 1)
{
printf("n Move disk 1 from tower %c to %c", s, d);
return;
}
TOH(n-1, s, i, d);
printf("n Move disk %d from tower %c to %c", n, s, d);
TOH(n-1, i, d, s);
}
10-16
A Classical Case: Towers of Hanoi
The execution result of calling TOH(n, 'A','C','B');
Unit 4
 Pointers and Arrays:
 Pointers and address, dynamic memory management,
Pointers and Function Arguments, Pointers and Arrays,
Address Arithmetic, character Pointers and Functions,
Pointer Arrays,
 Pointer to Pointer, Multi-dimensional array and Row/column
major formats, Initialization of Pointer Arrays, Command
line arguments, Pointer to functions, complicated declarations
and how they are evaluated
Pointers and address
int* pc, c;
c = 10;
pc = &c;
int* pc, c;
c = 20;
pc = &c;
c = 10;
printf("%d", c);
printf("%d", *pc);
int* pc, c, d;
c = 20;
d = 30;
pc = &c; printf("%d", *pc);
pc = &d; printf("%d", *pc)
What is a pointer
 A pointer is a variable that stores the memory address of
another variable.
int x=1, y=2;
int *ip;
ip = &x;
y = *ip;
x = ip;
*ip = 3
Pointers and address
int a = 10, b = 2;
int *p1, *p2;
p1 = &a;
p2 = &b;
printf("%pn %p n", p1, p2);
printf("%dn %d n", *p1, *p2);
OUTPUT
0xbfffdac4
0xbfffdac0
10
2
Add two numbers using pointers
int *ptr1, *ptr2;
int num;
printf("nEnter two numbers : ");
scanf("%d %d", ptr1, ptr2);
num = *ptr1 + *ptr2;
printf("Sum = %d", num);
Pointers and Function Arguments
Ex: 1
int* larger(int*, int*);
.......
int *p;
p = larger(&a, &b);
printf("%d is larger",*p);
.......
int* larger(int *x, int *y)
{
if(*x > *y) return x;
else return y; }
Ex: 2
void salaryhike(int *var, int b)
{
*var = *var+b;
}
......
salaryhike(&salary, bonus)
Pointer to Pointer
int i = 5, j = 6; k = 7;
int *ip1 = &i, *ip2 = &j;
int **ipp;
ipp = &ip1;
Pointer to Pointer: Example
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var;
pptr = &ptr;
printf("Value of var = %dn", var );
printf("Value available at *ptr = %dn", *ptr );
printf("Value available at **pptr = %dn", **pptr);
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
int x[4];
int i;
for(i = 0; i < 4; ++i)
printf("&x[%d] = %pn", i, &x[i]);
printf("Address of array x: %p", x);
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i)
{
scanf("%d", x+i);
sum += *(x+i);
}
printf("Sum = %d", sum);
Pointers and Arrays
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
ptr = &x[2];
printf("*ptr = %d n", *ptr);
printf("*(ptr+1) = %d n", *(ptr+1));
printf("*(ptr-1) = %d", *(ptr-1));
Pointers and Arrays
Operations on Pointers/Pointer Arithmetic
 Increment Pointers
 Decrement Pointers
 Compare Pointers
 Difference between Pointers
Increment Pointers
const int MAX = 3;
int main ()
{
int var [ ] = {10, 100, 200};
int i, *ptr;
ptr = var;
for ( i = 0; i < MAX; i++)
{
printf("Address of var[%d] = %xn", i, ptr );
printf("Value of var[%d] = %dn", i, *ptr );
ptr++;
}
return 0;
}
Address of var[0] = bfcf60ac
Value of var[0] = 10
Address of var[1] = bfcf60b0
Value of var[1] = 100
Address of var[2] = bfcf60b4
Value of var[2] = 200
Increment Pointers
int main()
{
int *ptr=(int *)1000;
ptr=ptr+3;
printf("New Value of ptr: %u",ptr);
}
New Value of ptr : 1012
It will store 1000 in the pointer variable
considering 1000 is memory location for any
of the integer variable.
Decrement Pointers: Example
const int MAX = 3;
int main ( )
{
int var[ ] = {10, 100, 200};
int i, *ptr;
ptr = &var[MAX-1];
for ( i = MAX; i > 0; i--)
{
printf("Address of var[%d] = %xn", i, ptr );
printf("Value of var[%d] = %dn", i, *ptr );
ptr--;
}
}
Address of var[3] = bff4fb44
Value of var[3] = 200
Address of var[2] = bff4fb40
Value of var[2] = 100
Address of var[1] = bff4fb3c
Value of var[1] = 10
Pointer Comparison
int *ptr1,*ptr2;
ptr1 = (int *)1000;
ptr2 = (int *)2000;
if(ptr2 > ptr1)
printf("Ptr2 is far from ptr1");
Ptr2 is far from ptr1
Difference between
pointers
int num , *ptr1 ,*ptr2 ;
ptr1 = &num ;
ptr2 = ptr1 + 2 ;
printf("%ld",ptr2 - ptr1);
2
Passing Pointers to a function
int main( )
{
int v1 = 11, v2 = 77 ;
printf("Before swapping:");
printf("nValue of v1 is: %d", v1);
printf("nValue of v2 is: %d", v2);
swapnum ( &v1, &v2 );
printf("nAfter swapping:");
printf("nValue of v1 is: %d", v1);
printf("nValue of v2 is: %d", v2);
}
swapnum ( int *num1, int *num2
)
{
int tempnum ;
tempnum = *num1 ;
*num1 = *num2 ;
*num2 = tempnum ;
}
Dynamic memory Allocation
malloc()
Allocates the memory of requested size and returns the
pointer to the first byte of allocated space.
calloc()
Allocates the space for elements of an array. Initializes
the elements to zero and returns a pointer to the memory.
realloc()
It is used to modify the size of previously allocated
memory space.
free() Frees or empties the previously allocated memory space.
malloc()
Syntax:
ptr = (castType*) malloc(size);
Example
ptr = (int*) malloc(10 * sizeof(int));
Example Program
int n,i,*ptr,sum=0;
// Accept n
ptr=(int*)malloc(n*sizeof(int));
// code to check if memory not allotted
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
calloc()
Syntax:
ptr = (castType*)calloc(n, size);
Example
ptr = (int*) calloc(8, sizeof(int));
Example Program
int n,i,*ptr,sum=0;
// Accept n
ptr=(int*)calloc(n,sizeof(int));
// code to check if memory not allotted
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
realloc()
Syntax:
ptr = realloc(ptr, x);
Example
ptr = (char *) malloc(10);
ptr = (char *) realloc(ptr,20);
Example Program
int *ptr,n,i;
n = 2;
ptr = malloc(n * sizeof(int));
*(ptr + 0) = 1; *(ptr + 1) = 2;
for(i = 0; i < n; i++)
printf("%dn",ptr[i]);
n = 5;
ptr = realloc(ptr, n * sizeof(int));
*(ptr + 2) = 3; *(ptr + 3) = 4;
*(ptr + 4) = 5;
for(i = 0; i < size; i++)
printf("%dn",ptr[i]);

C Recursion, Pointers, Dynamic memory management

  • 1.
    11Mar2022: Recursion, Pointers,Dynamic memory management PROGRAMMING FOR PROBLEM SOLVING (PPS) B.Tech I Sem CST
  • 2.
    Schedule Date Topic 11 Mar2022 Recursion, Pointers: Intro,Dynam mem.mgmt 17 Mar 2022 Pointer arrays,functions 24 Mar 2022 Multidime.arrays; Structures: Intro 25 Mar 2022 Structures: arrays, functions; Selfref.strc Date Topics 31 Mar 2022 Typedef,union 1 Apr 2022 Tutorial (Lab pgms) 7 Apr 2022 Files:open,clos e,read,write 8 Apr 2022 File error handlng
  • 3.
    Quiz & InternalLab exam dates Date Details 30 Mar 2022 (6.30 to 8.00pm) Quiz 3 & Quiz 4 6 Apr 2022 Internal Lab exam
  • 4.
    Today’s Topics  Recursion Pointers  Pointers, address, pointer to pointer  Initialization of pointer arrays  Dynamic memory management
  • 5.
    Recursion: process ofrepeating the similar steps. Recursive function: a function that calls itself. Recursive functions types : Direct and Indirect recursive functions
  • 6.
    Direct recursive A functionis said to be direct recursive if it calls itself directly. Ex: int fun1 (int n) { if (cond) return 1; else return (fun1(n-1)); } Indirect Recursive A function is said to be indirect recursive if it calls another function and this new function calls the first calling function again. Ex: int func1(int n) { if (cond1) return 1; else return func2(n); } int func2(int n) { return func1(n); } f1 f1 f2 fn …
  • 7.
    Sum of ‘n’natural numbers int sum(int n) { if (n!=0) return n+sum(n-1); else return n; } Factorial of given number int factorial(int n) { if(n==1) return 1; else return n*factorial(n-1); } Fibonacci series int fib(int n) { if(n==0) return 0; else if(n==1) return 1; else return(fib(n-1)+fib(n-2)); } GCD of two no’s int gcd(int m,int n) { if(n==0) return m; else return (gcd(n,m%n)); } Towers of Hanoi void TOH(n,s,d,i) { If(n==1) { Print s, d; return; } TOH(n-1,s,i,d); Print n,s,d TOH(n-1,i,d,s); }
  • 8.
    A Classical Case:Towers of Hanoi • The towers of Hanoi problem involves moving a number of disks (in different sizes) from one tower (or called “peg”) to another. – The constraint is that the larger disk can never be placed on top of a smaller disk. – Only one disk can be moved at each time – Assume there are three towers available.
  • 9.
  • 10.
    Towers of Hanoi voidTOH(int n, char s, char d, char i) { if (n == 1) { printf("n Move disk 1 from tower %c to %c", s, d); return; } TOH(n-1, s, i, d); printf("n Move disk %d from tower %c to %c", n, s, d); TOH(n-1, i, d, s); }
  • 11.
    10-16 A Classical Case:Towers of Hanoi The execution result of calling TOH(n, 'A','C','B');
  • 13.
    Unit 4  Pointersand Arrays:  Pointers and address, dynamic memory management, Pointers and Function Arguments, Pointers and Arrays, Address Arithmetic, character Pointers and Functions, Pointer Arrays,  Pointer to Pointer, Multi-dimensional array and Row/column major formats, Initialization of Pointer Arrays, Command line arguments, Pointer to functions, complicated declarations and how they are evaluated
  • 14.
    Pointers and address int*pc, c; c = 10; pc = &c; int* pc, c; c = 20; pc = &c; c = 10; printf("%d", c); printf("%d", *pc); int* pc, c, d; c = 20; d = 30; pc = &c; printf("%d", *pc); pc = &d; printf("%d", *pc)
  • 15.
    What is apointer  A pointer is a variable that stores the memory address of another variable.
  • 17.
    int x=1, y=2; int*ip; ip = &x; y = *ip; x = ip; *ip = 3
  • 18.
    Pointers and address inta = 10, b = 2; int *p1, *p2; p1 = &a; p2 = &b; printf("%pn %p n", p1, p2); printf("%dn %d n", *p1, *p2); OUTPUT 0xbfffdac4 0xbfffdac0 10 2
  • 19.
    Add two numbersusing pointers int *ptr1, *ptr2; int num; printf("nEnter two numbers : "); scanf("%d %d", ptr1, ptr2); num = *ptr1 + *ptr2; printf("Sum = %d", num);
  • 20.
    Pointers and FunctionArguments Ex: 1 int* larger(int*, int*); ....... int *p; p = larger(&a, &b); printf("%d is larger",*p); ....... int* larger(int *x, int *y) { if(*x > *y) return x; else return y; } Ex: 2 void salaryhike(int *var, int b) { *var = *var+b; } ...... salaryhike(&salary, bonus)
  • 21.
    Pointer to Pointer inti = 5, j = 6; k = 7; int *ip1 = &i, *ip2 = &j; int **ipp; ipp = &ip1;
  • 22.
    Pointer to Pointer:Example int var; int *ptr; int **pptr; var = 3000; ptr = &var; pptr = &ptr; printf("Value of var = %dn", var ); printf("Value available at *ptr = %dn", *ptr ); printf("Value available at **pptr = %dn", **pptr); Value of var = 3000 Value available at *ptr = 3000 Value available at **pptr = 3000
  • 23.
    int x[4]; int i; for(i= 0; i < 4; ++i) printf("&x[%d] = %pn", i, &x[i]); printf("Address of array x: %p", x); int i, x[6], sum = 0; printf("Enter 6 numbers: "); for(i = 0; i < 6; ++i) { scanf("%d", x+i); sum += *(x+i); } printf("Sum = %d", sum); Pointers and Arrays
  • 24.
    int x[5] ={1, 2, 3, 4, 5}; int* ptr; ptr = &x[2]; printf("*ptr = %d n", *ptr); printf("*(ptr+1) = %d n", *(ptr+1)); printf("*(ptr-1) = %d", *(ptr-1)); Pointers and Arrays
  • 25.
    Operations on Pointers/PointerArithmetic  Increment Pointers  Decrement Pointers  Compare Pointers  Difference between Pointers
  • 26.
    Increment Pointers const intMAX = 3; int main () { int var [ ] = {10, 100, 200}; int i, *ptr; ptr = var; for ( i = 0; i < MAX; i++) { printf("Address of var[%d] = %xn", i, ptr ); printf("Value of var[%d] = %dn", i, *ptr ); ptr++; } return 0; } Address of var[0] = bfcf60ac Value of var[0] = 10 Address of var[1] = bfcf60b0 Value of var[1] = 100 Address of var[2] = bfcf60b4 Value of var[2] = 200 Increment Pointers int main() { int *ptr=(int *)1000; ptr=ptr+3; printf("New Value of ptr: %u",ptr); } New Value of ptr : 1012 It will store 1000 in the pointer variable considering 1000 is memory location for any of the integer variable.
  • 27.
    Decrement Pointers: Example constint MAX = 3; int main ( ) { int var[ ] = {10, 100, 200}; int i, *ptr; ptr = &var[MAX-1]; for ( i = MAX; i > 0; i--) { printf("Address of var[%d] = %xn", i, ptr ); printf("Value of var[%d] = %dn", i, *ptr ); ptr--; } } Address of var[3] = bff4fb44 Value of var[3] = 200 Address of var[2] = bff4fb40 Value of var[2] = 100 Address of var[1] = bff4fb3c Value of var[1] = 10
  • 28.
    Pointer Comparison int *ptr1,*ptr2; ptr1= (int *)1000; ptr2 = (int *)2000; if(ptr2 > ptr1) printf("Ptr2 is far from ptr1"); Ptr2 is far from ptr1 Difference between pointers int num , *ptr1 ,*ptr2 ; ptr1 = &num ; ptr2 = ptr1 + 2 ; printf("%ld",ptr2 - ptr1); 2
  • 29.
    Passing Pointers toa function int main( ) { int v1 = 11, v2 = 77 ; printf("Before swapping:"); printf("nValue of v1 is: %d", v1); printf("nValue of v2 is: %d", v2); swapnum ( &v1, &v2 ); printf("nAfter swapping:"); printf("nValue of v1 is: %d", v1); printf("nValue of v2 is: %d", v2); } swapnum ( int *num1, int *num2 ) { int tempnum ; tempnum = *num1 ; *num1 = *num2 ; *num2 = tempnum ; }
  • 30.
    Dynamic memory Allocation malloc() Allocatesthe memory of requested size and returns the pointer to the first byte of allocated space. calloc() Allocates the space for elements of an array. Initializes the elements to zero and returns a pointer to the memory. realloc() It is used to modify the size of previously allocated memory space. free() Frees or empties the previously allocated memory space.
  • 31.
    malloc() Syntax: ptr = (castType*)malloc(size); Example ptr = (int*) malloc(10 * sizeof(int)); Example Program int n,i,*ptr,sum=0; // Accept n ptr=(int*)malloc(n*sizeof(int)); // code to check if memory not allotted for(i=0;i<n;++i) { scanf("%d",ptr+i); sum+=*(ptr+i); } printf("Sum=%d",sum); free(ptr);
  • 32.
    calloc() Syntax: ptr = (castType*)calloc(n,size); Example ptr = (int*) calloc(8, sizeof(int)); Example Program int n,i,*ptr,sum=0; // Accept n ptr=(int*)calloc(n,sizeof(int)); // code to check if memory not allotted for(i=0;i<n;++i) { scanf("%d",ptr+i); sum+=*(ptr+i); } printf("Sum=%d",sum); free(ptr);
  • 33.
    realloc() Syntax: ptr = realloc(ptr,x); Example ptr = (char *) malloc(10); ptr = (char *) realloc(ptr,20); Example Program int *ptr,n,i; n = 2; ptr = malloc(n * sizeof(int)); *(ptr + 0) = 1; *(ptr + 1) = 2; for(i = 0; i < n; i++) printf("%dn",ptr[i]); n = 5; ptr = realloc(ptr, n * sizeof(int)); *(ptr + 2) = 3; *(ptr + 3) = 4; *(ptr + 4) = 5; for(i = 0; i < size; i++) printf("%dn",ptr[i]);