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
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
+ 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.
+ 1
Thank you both