0

Array & pointer relationship

Please look the attached-code in c++. Do you think this is the right code: arr = arr + 2, if are itselt is the array of int variables? We know that a[n] = *(a+n). Further, statement arr = arr + 2 is a single statement, it is not a part of arr[i] = i+2; inside the for- loop scope.

25th Dec 2022, 4:04 PM
Oliver Pasaribu
Oliver Pasaribu - avatar
1 Respuesta
+ 1
Oliver Pasaribu did you forget to add the code link? If arr is the original array definition, then it would be bad practice to change its value (int arr[5]; arr = arr + 2;). Or if it is pointing to allocated heap space, again it would be bad practice to change arr if it is the only copy of the original address because you need the original address to free the space.
25th Dec 2022, 5:51 PM
Brian
Brian - avatar