New Want to Write for Computaholics ? if Yes Contact us at "contact@computaholics.in"

C Program To Calculate Sum Of The Array(1D-Array) Elements Using Pointers

C Program To Calculate Sum Of The Array(1D-Array) Elements Using Pointers

#include<stdio.h>

void main()
{

   int array[10];
   int i, sum = 0;
   int *p;

   printf("Enter 20 elements : \n");

   for (i = 0; i < 20; i++)     //Not use semicolon here otherwise loop terminate here and run only single time
   {
    scanf("%d\t", &array[i]);    //input from user
   }
   

   p = array; //array=&array[0]  Pointer always point to base address of array

   for (i = 0; i < 20; i++)
  {
      sum = sum + *p;    //initially p took value of array[0] in *p
      p++;   //array contiguis memory allocation so after increment pointer point to next address of array[1]..and so on
   }

   printf("sum of array elements : %d\n", sum);    //Print Output On screen

}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics