+ 7

Why is it legal to do aray+i (i is any integer) but not aray++

Lets say that aray is an array .We can do basic arithmetic functions like addition (+) and substraction(-) on that aray but using --/++ gives an erroes thats says that lvalue is required as ++/-- operand.GIven that array is like a pointer using aray+3,shouldn't it point to the 4the element of the array but its seems that it still points to the first element....Any clue anyone...:(

7th Jan 2018, 5:29 PM
Cyberspace Cyborg
Cyberspace Cyborg - avatar
3 Réponses
+ 1
Name of array is a pointer to its first element. So if you do operation like arr+1 you just add 1 to its address hence you get second element. But when you do something like arr++ you basicly say (arr=arr+1) that you wanna override whole array with its second element, and that is illegal.
7th Jan 2018, 5:50 PM
Jakub Stasiak
Jakub Stasiak - avatar
+ 4
I was hoping to give you a quick hint on lvalues but it turns out in c++ it's a little more involved (like whether "l" means "left" or object "locator" expression; it's changed over time). I'm linking this just in case you want a little more insight on the error message itself: http://en.cppreference.com/w/cpp/language/value_category
7th Jan 2018, 6:45 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
0_o how did i miss that...srsly thanks for that ;)
7th Jan 2018, 5:54 PM
Cyberspace Cyborg
Cyberspace Cyborg - avatar