0

Plz explain this code

#include <stdio.h> int main() { int arr[]={1,2,3,4,5}; int *p=arr; ++*p; p+=2; printf("%d",*p); return 0; }

3rd May 2020, 7:26 AM
Kawser
Kawser - avatar
1 Odpowiedź
+ 3
Here , 1.) arr[]={1,2,3,4,5}; is creating an array of size 5 and arr stores the base address of first element as know array allocates memory in contiguous locations. 2.) int *p=arr; assigns the base address to pointer 'p' where pointer is variable which can store address of other element. 3.) ++*p; does the value incrementation of p pointing address Here according to precedence of operators '*' operator comes first then ++ operator here therefore it increments value at address stored. 4.) As told earlier , p holds the address of arr so as 'p' is storing address p+=2; increments address by 2 i.e it goes to element 3 in array as elements are stored in continuous manner. 5.) Finally printf("%d",*p); Prints value of *p i.e , 3 I hope this helps you Kawser . If you are still not clear please ask me.Thank you.
3rd May 2020, 9:16 AM
Nikhil Maroju
Nikhil Maroju - avatar