+ 5

How does this output comes? In C

Please help me to understand how does this output comes.... And also tell me, how it was working.... Here that code..... https://code.sololearn.com/cIk4gM5PPF1u/?ref=app https://code.sololearn.com/cIk4gM5PPF1u/?ref=app

18th Oct 2020, 6:58 AM
Yogeshwaran P
Yogeshwaran P - avatar
5 Answers
+ 6
p[2] can be referred to as *(p+2)
18th Oct 2020, 7:03 AM
Arsenic
Arsenic - avatar
+ 10
You initialised an array and a pointer to store the address of integer variable. By using * operator we can access the value of a variable through a pointer. In your printf statement you accessed the value of address 2 of a[p] would give us the value of the variable stored in address 2 subsequently. Thus the answer comes out as 2.
18th Oct 2020, 7:06 AM
Aditya
Aditya - avatar
+ 5
Interesting! As Arsenic said, it seems that, by ommiting the *, it can reference the entire array instead. It also seems that the arrays a and p are linked, so if you do a[2] = 42 in the line before the printf statement, it will output 42.
18th Oct 2020, 7:07 AM
Rowsej
Rowsej - avatar
+ 4
Thanks to all....šŸ˜ŠšŸ™
18th Oct 2020, 8:25 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 3
an array name is a pointer itself, it points to the first element address, and to other elements as you increment it.
18th Oct 2020, 7:08 AM
Bahhaāµ£
Bahhaāµ£ - avatar