performance - C Pointer Arithmetic Efficiency -


i've seen explanations pointer arithmetic (eg. pointer arithmetic). wondering there real difference between:
given:

int* arr = (int*) malloc(sizeof(int) * 3); 

does:

&(arr[1]) 

and:

arr + 1 

differ in way, beside syntax. either technically more efficient? there context use pointer addiction on first? saw 1 example printing 1 1000 without loop or conditionals. in advance.

no, there's no difference, not in performance (unless we're talking compile times) because:

  • arr[1] equivalent *(arr + 1) and
  • in &(*(arr + 1)) neither dereference nor address operators (the c standard says explicitly, dereference doesn't occur in such cases), operators cancel each other out.

so, after parsing , ast building phases compiler ends arr + 1 in both cases.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -