I just recently came across a case where a is not same as a 1. Please analyze the below snippet of code :
INPUT 1 :
int a[] = {11, 12};
int *p = a 1;
cout << *p;
OUTPUT 1:
12
INPUT 2 :
int a[] = {11, 12};
int *p = a;
cout << *p;
OUTPUT 2 :
error: lvalue required as increment operand
Can somebody please explain to me the reason behind this problem ?
CodePudding user response:
In first Case: when you are passing name of the array i.e. a here , you are passing the address of first value stored in array so when : int *p = a 1; is executed it takes first value from array i.e 11 and then 1 is added to that value and output is 12 . this code wont give you any error as you are not changing anything about array or in array. In second case: you will get a compile time error " lvalue required as increment operand" . Here you are trying to increment the array and you cannot perform the increment operation on array.