Basic Pointer Examples in C

Here we will develop basic C program examples using the pointer. For example:- C Program to add Two Number Using Pointer, Find Area of Circle Using Pointer, Find Largest among three number using the pointer, Check Odd-Even using Pointer, C program examples Find Sum and average in Range using Pointer, Find Character is vowel or consonant using Pointer, C program examples to Swap Numbers in Cyclic Order using Pointer

C Program to add Two Number Using Pointer

#include<stdio.h>
int main()
{
  int a, b, c;
  
  int *pa, *pb, *pc;
  pa=&a, pb=&b, pc=&c;

  printf("Enter two number: ");
  scanf("%d %d", pa, pb); 
  // pa and pb have address of a & b respectively

  *pc = *pa + *pb;
  printf("Sum=%d\n",*pc);

  return 0;
}

Output:-

Enter two number: 10,20
10 + 20 = 30

pa, pb, and pc point to the address of three variables a, b, and c. After addition, the sum value will be stored in variable pc. Similarly, we can do Subtraction, Multiplication, and division

Add two Number Using Pointer & Function

Same task can be done using function also. Address of variables a, b, and c are passed to function.

#include<stdio.h>
int add(int *a, int *b, int *c);
int main()
{
  int num1, num2, sum;
  int *pnum1, *pnum2, *psum;
  pnum1= &num1, pnum2= &num2, psum= &sum;

  printf("Enter two number: ");
  scanf("%d %d", pnum1, pnum2);

  add(pnum1, pnum2, psum);
  printf("%d + %d = %d\n", *pnum1, *pnum2, *psum);

  return 0;
}

int add(int *a, int *b, int *c)
{
  *c = *a + *b;
}

C Program to find Area of Circle Using Pointer

If the radius of a circle is r then Area is given by ℼ*r2

#include<stdio.h>
#define PI 3.14
int main()
{
  double r, area;
  double *pr, *parea;
  pr= &r, parea=&area;

  printf("Enter radius: ");
  scanf("%lf",pr);

  *parea = PI*(*pr)*(*pr);
  printf("Area of radius %.3lf = %.3lf\n", *pr, *parea);

  return 0;
}

Output:-

Enter radius: 9.7
Area of radius 9.700000 = 295.44

In Similar way we can also find area of Triangle, and Rectangle.

Find Area of Circle Using Pointer & Function

#include<stdio.h>
#define PI 3.14
int circlearea(double *r, double *area);
int main()
{
  double r, area;
  double *pr, *parea;
  pr= &r, parea= &area;

  printf("Enter radius: ");
  scanf("%lf",pr);

  circlearea(pr, parea);
  printf("Area of radius %lf = %.2lf\n",*pr, *parea);

  return 0;
}

int circlearea(double *r, double *area)
{
  *area = PI * *r * *r;
}

C Program to find Largest among three number using the pointer

#include<stdio.h>
int main()
{
  float x, y, z;
  float *px, *py, *pz;
  px=&x, py=&y, pz=&z;

  printf("Enter three number:");
  scanf("%f %f %f", px, py, pz);

  if(*px > *py)
  {
    if(*px > *pz)
      printf("Biggest = %.2f", *px);

    else
      printf("Biggest = %.2f", *pz);
  }

  else
  {
    if(*py > *pz)
      printf("Biggest = %.2f", *py);

    else
      printf("Biggest = %.2f", *pz);
  }

  return 0;
}

Output:-

Enter three number: 45 5 52
Largest = 52

We can use AND (&) operator, which will be short form to Find Largest among Three Number.

Largest among three number using pointer & function

#include<stdio.h>
void find(int *x, int *y, int *z);
int main()
{
  int a, b, c;
  int *pa, *pb, *pc;
  pa= &a, pb= &b, pc=&c;

  printf("Enter three number: ");
  scanf("%d %d %d", pa, pb, pc);

  find(pa, pb, pc);
  return 0;
}

void find(int *x, int *y, int *z)
{
  if( (*x > *y) && (*x > *z) )
    printf("Largest = %d", *x);

  else if((*y > *x) && (*y > *z))
    printf("Largest = %d", *y);

  else
    printf("Largest = %d", *z);
}

C Program to Check Odd-Even using Pointer

There are various methods to check Odd-Even. But, it can be solved using pointer also.

#include<stdio.h>
int main()
{
  int num, rem;
  int *pnum;
  pnum= &num;

  printf("Enter number: ");
  scanf("%d",pnum); //pnum has address of num

  rem= *pnum % 2;

  if(rem==0)
    printf("%d is even.\n", *pnum);

  else
    printf("%d is odd.\n", *pnum);

  return 0;
}

Output:-

Enter number: 9
9 is odd.

C Program to Find Sum and average in Range using Pointer

Here we will find the Sum and Average (in Range) from number m to number n. We can initialize values of m and n directly as m=1 and n=10 or take values of m and n entered from the user. First, we will find the sum, and after that average. pm and pn will store the address of variables m and n.

#include<stdio.h>
int main()
{
  int m, n, i;
  int *pm, *pn;
  pm= &m, pn= &n;

  double sum=0.0, avg;
  double *psum, *pavg;
  psum= &sum, pavg= &avg;

  printf("Enter range (m and n values)(m<n): ");
  scanf("%d %d", pm, pn);

  for(i=m;i<=n;i++)
  {
    *psum += i;
  }

  *pavg= (*psum) / (*pn - *pm + 1);
  printf("Sum= %.2lf, Average= %.2lf", *psum, *pavg);

  return 0;
}

Output:-

Enter range (m and n values)(m<n): 10,20
Sum= 165.00, Average= 15.00

Find Sum & average in Range using Pointer & Function

#include<stdio.h>
double calculate(int *m, int *n, double *sum, double *avg);
int main()
{
  int m, n;
  int *pm, *pn;
  pm= &m, pn= &n;

  double sum=0.0, avg;
  double *psum, *pavg;
  psum= &sum, pavg= &avg;

  printf("Enter m & n Values(m<n): ");
  scanf("%d %d", pm, pn);

  calculate(pm, pn, psum, pavg);
  printf("Sum= %.2lf and average= %.2lf\n", *psum, *pavg);

  return 0;
}

double calculate(int *m, int *n, double *sum, double *avg)
{
  int i;

  for(i=*m; i<=*n; i++)
  {
    *sum += i;
  }

  *avg = (*sum) / (*n -*m + 1);
}

C Program to find Character is vowel or consonant using Pointer

In English language A, E, I, O, U and a, e, i, o, u defined as vowel and Except all are consonants. We can check vowel and consonant using various ways.

#include<stdio.h>
int main()
{
  char c, *pc;
  pc= &c;

  printf("Enter a character: ");
  scanf("%c", pc);

  if( (*pc=='A'||*pc=='E'||*pc=='I'||*pc=='O'||*pc=='U') ||
      (*pc=='a'||*pc=='e'||*pc=='i'||*pc=='o'||*pc=='u') )
    printf("%c is Vowel.\n", *pc);

  else
    printf("%c is consonant.\n", *pc);

  return 0;
}

Output:-

Enter a character: G
G is consonant.

C Program to swap Numbers in Cyclic Order using Pointer

If The Numbers are 3,5,9 then After Swapping its order becomes 9,3,5
We are using the pointer so, we will use call by reference.

#include<stdio.h>
void swap(int *p, int *q, int *r);
int main()
{
  int a, b, c;
  int *pa, *pb, *pc;
  pa=&a, pb=&b, pc=&c;

  printf("Enter three integer: ");
  scanf("%d %d %d",pa,pb,pc);

  printf("Value Before Swapping:\n");
  printf("a=%d \t b=%d \t c=%d\n", *pa, *pb, *pc);

  swap(pa,pb,pc);

  printf("Value After Swapping:\n");
  printf("a=%d \t b=%d \t c=%d\n", *pa, *pb, *pc);

  return 0;
}

void swap(int *p, int *q, int *r)
{
  int temp;

  temp = *p;
  *p = *r;
  *r = *q;
  *q = temp;
}

Output:-

Enter three integer: 5 6 7
Value Before Swapping:
a=5 b=6 c=7
Value After Swapping:
a=7 b=5 c=6

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *