Basic Example using Pointer and Array in C++


Example Program:

Here is the simple program for printing the basic examples using pointer and array in C++.

Source Code Example : 1

#include<iostream>
using namespace std;
int main()
{
  float arr[3];
  float *ptr;
  cout<<"Displaying address using arrays: "<<endl;
  for(int i=0;i<3;++i)
  {
    cout<<"&arr["<<i<<"] = "<<&arr[i]<<endl;
  }
  ptr=arr;
  cout<<"\nDisplaying address using pointers: "<<endl;
  for(int i=0;i<3;++i)
  {
    cout<<"ptr + "<<i<<" = "<<ptr+i<<endl;
  }
  return 0;
}
To download raw file Click Here

Output

Displaying address using arrays: 
&arr[0] = 0x7ffd0f0ca02c
&arr[1] = 0x7ffd0f0ca030
&arr[2] = 0x7ffd0f0ca034

Displaying address using pointers: 
ptr + 0 = 0x7ffd0f0ca02c
ptr + 1 = 0x7ffd0f0ca030
ptr + 2 = 0x7ffd0f0ca034

Source Code Example : 2

#include<iostream>
using namespace std;
const int MAX = 3;
int main()
{
  int var[MAX] = {10,100,200};
  int *ptr;
  ptr=var;
  for(int i=0;i<MAX;i++)
  {
    cout<<"Address of var["<<i<<"] = ";
    cout<<ptr<<endl;
    cout<<"Value of var["<<i<<"] = ";
    cout<<*ptr<<endl;
    ptr++;
  }
  return 0;
}
To download raw file Click Here

Output

Address of var[0] = 0x7fffd226f93c
Value of var[0] = 10
Address of var[1] = 0x7fffd226f940
Value of var[1] = 100
Address of var[2] = 0x7fffd226f944
Value of var[2] = 200


Basic Programs


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes