0

[Solved]Explain the functioning of the following code.

Guys today I was playing C challenges with one of my sololearn friends and i got stuck in a question And finally I answered the question wrong but the main thing was that I can't understand the code even after knowing the correct answer. https://code.sololearn.com/cC3zR9mcgg9h/?ref=app

13th Jul 2020, 9:50 AM
Akshay Panwar
Akshay Panwar - avatar
3 Respostas
+ 5
#include <stdio.h> int main() { int arr[]={2,2,3,4,5}; int *p=arr; printf("%p %d",p,++*p); ++*p; p+=2; printf("%d",*p); } the ++*p seem to confuse you but has no effect on the output because it only increment the value at the address and not the address itself what really matter is the p+=2 which push the address to the third place, try run the edit I made that should help you understand it better
13th Jul 2020, 10:23 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
int arr[]={1,2,3,4,5}; int *p=arr; //++*p; p+=2; printf("%d",*p); arr is holding address of first element of int array... In other words, array name is nothing but address of first element.... ++*p does not affect output and you can comment also and observe there is no change in output. as p points to first element, p = p+2 (similar to p+=2) would point to third element of array... * Means we are getting value...so output is 3 as third element in array is 3.
13th Jul 2020, 10:24 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Thank you both
13th Jul 2020, 4:18 PM
Akshay Panwar
Akshay Panwar - avatar