0
Can anyone explain this code?
6 Respuestas
+ 2
For output 20 you must write *(ptr+1)
You must increment the pointer to get the next value in the array but you cannot do that by increasing or decreasing the value pointed by the pointer.
+ 2
Thanks all for explaining me this 👍
+ 1
Preity
Kit Delano Cat was right, the warning actually tells you already, that output is undefined (or unpredictable). Output will differ between compilers and internal implementation.
Any attempt to modify a data as well as accessing the data in a single point of execution sequence may lead to undefined behaviour. If I understood it correctly.
0
You have an integer pointer pointing to the array. Always remember the array name consist of the base address of the array. So int* ptr = arr; means ptr now is pointing to the first value of the array. So in the printf statement we use the dereferencing operator to retrieve the value pointed by ptr. We know that is 10, and you should understand that *ptr++ will also give 10 since it is post incremented. If you would have wrote ++*ptr then it would result in 11.