Sometimes i see different array-access styles in C++ and thought it could be relative to an assembly addressing mode:
C++:
int * aa=new int[2];
0[aa]=15; //a little different than aa[0]
1[aa]=15;
aa[0]=15;
aa[1]=15;
printf("%d %d \n",aa[0],aa[1]);
Assembly:
__asm
{
mov aa[0],ebx
mov aa[1],eax
mov 0[aa],ebx
mov 1[aa],eax
}
Is this C++ array-access notation a standard and if yes, was it derived from an assembly addressing mode?
When i try [aa]1=5; , compiler gives
- “aa attribute not found”,
- “missing ‘;’ before ‘constant'”
-
“left operand must be l-value”.
//When i try pointer arithmetic,
*(aa+1)=0//gives no error
*(aa+0)=0//gives no error 🙂
Is this rule same for the operator [] overloading?
MSVC++ 2010
Thank you.
This is a well-known quirk of C and C++, they don’t really care which half of an expression goes in the square brackets
[]. They both evaluate to the same equivalent expression: