2

//why *var=i is valid and var++ is not valid

#include<iostream>

using namespace std;
const int MAX = 3;

int main () {
   int  var[MAX] = {10, 100, 200};

   for (int i = 0; i < MAX; i++) {
      *var = i;    // This is a correct syntax
      var++;       // This is incorrect.
   }

return 0;
}

// can anyone elaborate this in terms of pointers vs arrays in c++

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
psr-5511
  • 29
  • 1
  • Inheritance from C makes that post from the C FAQ relevant here: [Is an array name a pointer?](https://stackoverflow.com/q/1641957/3545273) – Serge Ballesta Jun 25 '21 at 06:28
  • 1
    `var` is an array. The name of an array can implicitly converted to a pointer (to its first element) in some contexts. So `*var = i` is equivalent to `var[0] = i`. However, `var` is actually an array and not actually a pointer, so operations on it that change its value - like `var++` are invalid. – Peter Jun 25 '21 at 08:20

0 Answers0