subtract from array using pointers c++ code example

Example: subtract from array using pointers c++

#include<iostream>

int main()
{								
  								    // [0], [1], [2], [3], [4]
  char array_my_name [] {"amir"};  //   a,   m    i     r   null
  
  char *char_ptr1{nullptr};
  char *char_ptr2{nullptr};
  
  char_ptr1 = &array_my_name[1];
  char_ptr2 = &array_my_name[3];
  									//amir
  std::cout<<"in the string, "<< array_my_name 
    					 //r 						2
  <<"the charachter "<<*char_ptr2<<" is "<<(char_ptr2-char_ptr1)
    							//m
  <<"charchters away from "<<*char_ptr1<<std::endl;
  
  return 0;
}

Tags:

Cpp Example