0% found this document useful (0 votes)
39 views8 pages

Pointers Programs

The document discusses various ways to use pointers in C programming. It provides 16 code examples demonstrating pointer fundamentals like passing pointers to functions, pointer arithmetic, NULL pointers, and more. The examples cover calculating areas and factorials, reversing numbers, linear search, strings and arrays, and common pointer operations.

Uploaded by

yashashvipal11d
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)
39 views8 pages

Pointers Programs

The document discusses various ways to use pointers in C programming. It provides 16 code examples demonstrating pointer fundamentals like passing pointers to functions, pointer arithmetic, NULL pointers, and more. The examples cover calculating areas and factorials, reversing numbers, linear search, strings and arrays, and common pointer operations.

Uploaded by

yashashvipal11d
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/ 8

Pointers

1) WAP to find the area of circle using pointers


#include<stdio.h>
int main()
{
double radius,area=0.0;
double *pradius=&radius,*parea=&area;
printf("\n Enter the radius of the circle:");
scanf("%lf",pradius);
*parea=3.14*(*pradius)*(*pradius);
printf("\n The area of the circle with radius %.2lf = %.2lf",*pradius,*parea);
return 0;
}
2) WAP to calculate factorial of a number using pointers
#include<stdio.h>
int main()
{
int i,n,fact=1;
int *pn,*pfact;
pn=&n;
pfact=&fact;
printf("\n Enter number:");
scanf("%d",pn);
for(i=1;i<=*pn;i++)
{
*pfact=*pfact*i;
}
printf("\n Factorial of number is:%d",*pfact);
return 0;
}
3) WAP to display reverse of a number using pointers
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
int *pn,*prn,*pr;
pn=&n;
prn=&reversedNumber;
pr=&remainder;
printf("Enter an integer: ");
scanf("%d", pn);
while(*pn != 0)
{
*pr = *pn%10;
*prn = *prn*10 + *pr;
*pn = *pn/10;
}
printf("Reversed Number = %d",*prn);
return 0;
}
4) WAP to calculate the sum of all numbers from m and n using pointers
#include<stdio.h>
int main()
{
int num,*pnum=&num;
int m,*pm=&m;
int n,*pn=&n;
int sum=0,*psum=&sum;
float avg,*pavg=&avg;
printf("\n Enter the starting and ending limit of the numbers to be
summed:");
scanf("%d %d",pm,pn);
while(*pm<=*pn)
{
*psum=*psum+*pm;
*pm=*pm+1;
}
printf("\n Sum of numbers=%d",*psum);
return 0;
}
5) WAP to read and display elements of 1D array using pointer to an array
#include<stdio.h>
int main()
{
int i,n;
int a[10],*parr=a;
printf("\n Enter the number of elements:");
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",parr+i);
}
printf("\n Entered array elements are:");
for(i=0;i<n;i++)
{
printf("\t %d",*(parr+i));
}
return 0;
}
6) WAP to find the sum and mean of integer 1D array elements using pointer to an
array
#include<stdio.h>
int main()
{
int i,n,arr[20],sum=0;
int *pn=&n,*parr=arr,*psum=&sum;
float mean=0.0,*pmean=&mean;
printf("\n Enter the number of elements in the array:");
scanf("%d",pn);
for(i=0;i<*pn;i++)
{
printf("\n Enter the number:");
scanf("%d",(parr+i));
}
for(i=0;i<*pn;i++)
{
*psum=*psum+*(arr+i);
}
*pmean=*psum/ *pn;
printf("\n The numbers you entered are:");
for(i=0;i<*pn;i++)
printf("\n%d",*(arr+i));
printf("\n The sum is:%d",*psum);
printf("\n The mean is:%f",*pmean);
return 0;
}
7) WAP to perform linear search using pointer to an array
#include<stdio.h>
int main()
{
int arr[20],i,n,element,loc=-1;
int *ptr=arr;
printf("\n Enter number of elements:");
scanf("%d",&n);
printf("\n Enter values of elements:");
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
}
printf("\n Enter the element to search:");
scanf("%d",&element);
for(i=0;i<n;i++)
{
if(*(ptr+i)==element)
{
loc=i;
break;
}
}
if(loc==-1)
{
printf("\n Element is not found");
}
else
{
printf("\n Element found at location:%d",loc+1);
}
return 0;
}
8) WAP to swap the values of two variables using temporary variable after passing
pointer to a function
//Passing arguments to function using pointers
#include<stdio.h>
void swap(int *,int *);
int main()
{
int a,b;
printf("\n Enter the first number:");
scanf("%d",&a);
printf("\n Enter the second number:");
scanf("%d",&b);
swap(&a,&b);
printf("\n Values of a and b after swap are:%d,%d",a,b);
return 0;
}
void swap(int *x,int *y)
{
int temp;
int *ptemp=&temp;
*ptemp=*x;
*x=*y;
*y=*ptemp;
}
9) WAP to display the reverse of a number after passing pointer to a function
#include<stdio.h>
void reverse(int*);
int main()
{
int num;
printf("\n Enter number");
scanf("%d",&num);
reverse(&num);
return 0;
}
void reverse(int *px)
{
int rem,rev=0;
int *prem=&rem,*prev=&rev;
while(*px!=0)
{
*prem=*px%10;
*prev=*prev*10+*prem;
*px=*px/10;
}
printf("\n Reverse of the number is:%d",*prev);
}
10) WAP to find the sum of array elements after passing array to a function
using pointer
#include<stdio.h>
void addition(int *,int);
int main()
{
int a[]={1,2,3,4,5};
addition(a,5);
return 0;
}
void addition(int *ptr,int size)
{
int sum=0,i;
for(i=0;i<size;i++)
{
sum=sum+*(ptr+i);
}
printf("\n Sum of array elements is is:%d",sum);

}
11) WAP to traverse a string using pointer
#include<stdio.h>
int main()
{
char *g="C Programming";
int length=0,i=0;
while(*g!='\0')
{
printf("%c",*g);//Value at address
g++;//Pointer is incremented by 1 after each iteration
length++;//Variable for counting length
}
printf("\nLength of the string is:%d",length);
return 0;
}
12) WAP to demonstrate wild pointer
#include<stdio.h>
int main()
{
int *ptr;//Wild pointer
int a=10;
//printf("%x",ptr);//Gives gargage address value
//printf("\n%d",*ptr);//Gives garbage value stored in the grabage address
ptr=&a;//Now ptr is not a wild pointer
printf("\n%d",*ptr);//
return 0;
}
13) WAP to demonstrate NULL pointer
#include<stdio.h>
int main()
{

int *ptr=NULL;
int a=10;
printf("%x",ptr);
//printf("%d",*ptr);//---Not allowed(Dereferencing NULL pointer)
ptr=&a;
printf("\n%d",*ptr);
return 0;
}
14) WAP to demonstrate generic pointer(or void pointer)
#include<stdio.h>
int main()
{
int x=10;
char ch='A';
void *gp;
gp=&x;
printf("\n Generic pointer points to the integer value=%d",*(int*)gp);
gp=&ch;
printf("\n Generic pointer now points to the character %c",*(char*)gp);
return 0;
}
15) WAP to demonstrate dangling pointer
// Deallocating a memory pointed by ptr causes dangling pointer
#include <stdlib.h>
#include <stdio.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int));
// After below free call, ptr becomes a
// dangling pointer
printf("%u",ptr);
free(ptr);
printf("\n%u",ptr);//dangling pointer
// No more a dangling pointer(Solution)
// ptr = NULL;
}
16) WAP to demonstrate constant pointer
#include<stdio.h>
int main()
{
int var1 = 60, var2 = 0;
int *const ptr = &var1;
//ptr = &var2;
printf("%d\n", *ptr);
return 0;
}
Practice questions to do:
WAP to implement insertion and deletion using pointer to an array concept
WAP to find simple interest using pointers
WAP to implement binary search using pointer to an array concept
WAP to check whether the entered integer is prime or composite after passing
pointer to function
WAP to display largest of three numbers using pointers
WAP to display smallest of three numbers after returning pointer from function
WAP to calculate area of triangle using pointers
WAP to check whether the entered number is palindrome or not using pointers
WAP to display the sum of odd array elements after passing array to a function using
pointer
WAP to swap the values of two variables using pointers
WAP to count vowels in a given string using pointer

You might also like