Table of Contents
Practice programs on Pointers
1. Program in C to demonstrate how to handle the pointers in the program:
#include <stdio.h> int main() { int* ab; int m; m=29; printf("\n\n Pointer : How to handle the pointers in the program :\n"); printf("------------------------------------------------------------\n"); printf(" Here in the declaration ab = int pointer, int m= 29\n\n"); printf(" Address of m : %p\n",&m); printf(" Value of m : %d\n\n",m); ab=&m; printf(" Now ab is assigned with the address of m.\n"); printf(" Address of pointer ab : %p\n",ab); printf(" Content of pointer ab : %d\n\n",*ab); m=34; printf(" The value of m assigned to 34 now.\n"); printf(" Address of pointer ab : %p\n",ab); printf(" Content of pointer ab : %d\n\n",*ab); *ab=7; printf(" The pointer variable ab is assigned the value 7 now.\n"); printf(" Address of m : %p\n",&m);//as ab contain the address of m //so *ab changed the value of m and now m become 7 printf(" Value of m : %d\n\n",m); return 0; }
*********************************************************************
2. Add two numbers using call by reference:
#include <stdio.h> long addTwoNumbers(long *, long *); int main() { long fno, sno, sum; printf("\n\n Pointer : Add two numbers using call by reference:\n"); printf("-------------------------------------------------------\n"); printf(" Input the first number : "); scanf("%ld", &fno); printf(" Input the second number : "); scanf("%ld", &sno); sum = addTwoNumbers(&fno, &sno); printf(" The sum of %ld and %ld is %ld\n\n", fno, sno, sum); return 0; } long addTwoNumbers(long *n1, long *n2) { long sum; sum = *n1 + *n2; return sum; }
**************************************************************
3.Generate permutations of a given string:
#include <stdio.h> #include <string.h> void changePosition(char *ch1, char *ch2) { char tmp; tmp = *ch1; *ch1 = *ch2; *ch2 = tmp; } void charPermu(char *cht, int stno, int endno) { int i; if (stno == endno) printf("%s ", cht); else { for (i = stno; i <= endno; i++) { changePosition((cht+stno), (cht+i)); charPermu(cht, stno+1, endno); changePosition((cht+stno), (cht+i)); } } } int main() { char str[] = "abcd"; printf("\n\n Pointer : Generate permutations of a given string :\n"); printf("--------------------------------------------------------\n"); int n = strlen(str); printf(" The permutations of the string are : \n"); charPermu(str, 0, n-1); printf("\n\n"); return 0; }
*******************************************************
4. Store and retrieve elements from an array:
#include <stdio.h> int main() { int arr1[25], i,n; printf("\n\n Pointer : Store and retrieve elements from an array :\n"); printf("------------------------------------------------------------\n"); printf(" Input the number of elements to store in the array :"); scanf("%d",&n); printf(" Input %d number of elements in the array :\n",n); for(i=0;i<n;i++) { printf(" element - %d : ",i); scanf("%d",arr1+i); } printf(" The elements you entered are : \n"); for(i=0;i<n;i++) { printf(" element - %d : %d \n",i,*(arr1+i)); } return 0; }
*****************************************************
5. Calculate the length of a string:
#include <stdio.h> int calculateLength(char*); void main() { char str1[25]; int l; printf("\n\n Pointer : Calculate the length of the string :\n"); printf("---------------------------------------------------\n"); printf(" Input a string : "); fgets(str1, sizeof str1, stdin); l = calculateLength(str1); printf(" The length of the given string %s is : %d ", str1, l-1); printf("\n\n"); } int calculateLength(char* ch) // ch = base address of array str1 ( &str1[0] ) { int ctr = 0; while (*ch != '\0') { ctr++; ch++; } return ctr; }
********************************************************************
6. Find the factorial of a given number:
#include <stdio.h> void findFact(int,int*); int main() { int fact; int num1; printf("\n\n Pointer : Find the factorial of a given number :\n"); printf("------------------------------------------------------\n"); printf(" Input a number : "); scanf("%d",&num1); findFact(num1,&fact); printf(" The Factorial of %d is : %d \n\n",num1,fact); return 0; } void findFact(int n,int *f) { int i; *f =1; for(i=1;i<=n;i++) *f=*f*i; }
****************************************************************
7. Sort an array using pointer:
#include <stdio.h> void main() { int *a,i,j,tmp,n; printf("\n\n Pointer : Sort an array using pointer :\n"); printf("--------------------------------------------\n"); printf(" Input the number of elements to store in the array : "); scanf("%d",&n); printf(" Input %d number of elements in the array : \n",n); for(i=0;i<n;i++) { printf(" element - %d : ",i+1); scanf("%d",a+i); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if( *(a+i) > *(a+j)) { tmp = *(a+i); *(a+i) = *(a+j); *(a+j) = tmp; } } } printf("\n The elements in the array after sorting : \n"); for(i=0;i<n;i++) { printf(" element - %d : %d \n",i+1,*(a+i)); } printf("\n"); }
***************************************************************
8. Show a function returning pointer:
#include <stdio.h> int* findLarger(int*, int*); void main() { int numa=0; int numb=0; int *result; printf("\n\n Pointer : Show a function returning pointer :\n"); printf("--------------------------------------------------\n"); printf(" Input the first number : "); scanf("%d", &numa); printf(" Input the second number : "); scanf("%d", &\numb); result=findLarger(&numa, &numb); printf(" The number %d is larger. \n\n",*result); } int* findLarger(int *n1, int *n2) { if(*n1 > *n2) return n1; else return n2; }
****************************************************************
9. Sum of all elements in an array:
#include <stdio.h> void main() { int arr1[10]; int i,n, sum = 0; int *pt; printf("\n\n Pointer : Sum of all elements in an array :\n"); printf("------------------------------------------------\n"); printf(" Input the number of elements to store in the array (max 10) : "); scanf("%d",&n); printf(" Input %d number of elements in the array : \n",n); for(i=0;i<n;i++) { printf(" element - %d : ",i+1); scanf("%d",&arr1[i]); } pt = arr1; // pt store the base address of array arr1 for (i = 0; i < n; i++) { sum = sum + *pt; pt++; } printf(" The sum of array is : %d\n\n", sum); }
****************************************************************
10. Print all the alphabets:
#include <stdio.h>; int main() { char alph[27]; int x; char *ptr; printf("\n\n Pointer : Print all the alphabets:\n"); printf("----------------------------------------\n"); ptr = alph; for(x=0;x<26;x++) { *ptr=x+'A'; ptr++; } ptr = alph; printf(" The Alphabets are : \n"); for(x=0;x<26;x++) { printf(" %c ", *ptr); ptr++; } printf("\n\n"); return(0); }
****************************************************************************
For more practice questions, go to Questionnare.