Simple Program for Sum of Integer an array using pointers in C++


Syntax

pointer_variable = &var[position];

pointer_variable++ is increasing Address value based on Data Type
pt++; --> Its location next position of Array

Simple Program for Sum of Integer an array using pointers

/* Simple Program for Sum of Integer in an array using pointers in C++*/
/* Pointer and Array C++ Program,C++ Pointer Examples */

// Header Files
#include <iostream>
#include<conio.h>

using namespace std;

#define MAX_SIZE 5

int main() {

   // Declare Variables
   int var[] = {10, 20, 30, 40, 50};
   int i = 0, sum = 0;

   //Pointer Variable Declaration for Integer Data Type 
   int *pt;

   cout << "Pointer Example C++ Program : Sum of Integer Pointer and Array \n";

   //& takes the address of var , Here now pt == &var, so *pt == var
   pt = &var[0];

   while (i < MAX_SIZE) {
      i++;

      // Calculate sum using pointer
      sum = sum + *pt;

      // pt++ is increasing Address value based on Data Type
      pt++;
   }

   cout << "Sum of Array : " << sum;

   getch();
   return 0;
}

Sample Output

Pointer Example C++ Program : Sum of Integer Pointer and Array

Sum of Array : 150