Built-in arraysArray names are converted to (constant)pointers2ainta[5]=(1,2,4,8,16];intb =*a;ll ok.equivalenttointb=ajol:int c=*(a + 3);ll okequivalent to int c=a3l;pointer arithmetic
Built-in arrays int a[5] = {1, 2, 4, 8, 16}; Array names are converted to (constant) pointers. int c = *(a + 3); // ok. equivalent to int c = a[3]; pointer arithmetic int b = *a; // ok. equivalent to int b = a[0]; a a
Built-in arraysArray names are converted to (constant)pointers2ainta[5]=(1,2,4,8,16];//error:不能用int(*)[5]初始化int**int **p=&a;//ok.int (*q)[5] =&a;I/oktheresultis1.cout<<**q<<endl;I/ok.theresultis 16cout<<*(*q +4)<<endl;
Built-in arrays Array names are converted to (constant) pointers. int **p = &a; int (*q)[5] = &a; cout << **q << endl; cout << *(*q + 4) << endl; // error: 不能用int(*)[5]初始化int ** a a // ok. // ok. the resultis 1. // ok. the resultis 16. int a[5] = {1, 2, 4, 8, 16};
Array index out of boundsinta[5]={1,2, 4,8, 16];cout<<a[5];l/ warning: array index 5 is past the end of the arraycout<<*(a + 5);l/ problematic,but will be accepted by the compilerAll warnings should betreated as errors
Array index out of bounds int a[5] = {1, 2, 4, 8, 16}; cout << a[5]; // warning: array index 5 is past the end of the array cout << *(a + 5); // problematic, but will be accepted by the compiler All warnings should be treated as errors
Bounds checkingconst size_t N= 5;inta[N] = {1,2,4, 8,16];size_tj= 0;size_tj=0;/ /...some other code// ...some other codeif (j>= 0 &&j<N)[assert (j>=0&&j<N);//dosomethingwithelementaljl//dosomethingwithelementali]1else (//reportan index-out-of-bound error1
Bounds checking const size_t N = 5; int a[N] = {1, 2, 4, 8, 16}; size_t j = 0; // . some other code if (j >= 0 && j < N) { // do something with element a[j] } else { // report an index-out-of-bound error } size_t j = 0; // . some other code assert (j >= 0 && j < N); // do something with element a[j]