0

Can anyone explain this....what is the output of this ...and explain logic n answer behind this?

#include <iostream> using namespace std; int main() { int array[]={10,20,30}; cout<<-2[array]; return 0; }

4th Jan 2021, 5:57 AM
Disha Jain
2 Réponses
+ 7
Let me translate the lines inside main() to english :- 1) int array[]={10,20,30}; Create an array with name "array" and put elements {10,20,30} in it in from the begining. 2) cout<<-2[array]; which is similar as cout<<-array[2]; Means get the element at 2nd index of array(which is 30 as index start from 0) and print it after inverting it's sign Thus the output will be -30.
4th Jan 2021, 6:10 AM
Arsenic
Arsenic - avatar
+ 3
This is an alternative sytax for reading a value from an array. The 2 in front of [array] in the cout line is actually the array index. The Typical syntax would be array[2]. The value at index 2 is 30. As there is a negative sign preceeding the call to the array, the negative is applied to the output, giving the final output as: -30 I hope this helps.
4th Jan 2021, 6:12 AM
Elizabeth Kelly
Elizabeth Kelly - avatar