Some one explain this code plz,^^ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Some one explain this code plz,^^

pointers and "&" https://code.sololearn.com/cg72o4hzX0gC/?ref=app

21st Jul 2018, 12:24 PM
Anas Sanba
23 Answers
+ 9
yes you are right KrOW as I set a code if you try it in turbo cpp4 u will get answer as 1 0 1 1 1 but in gcc and in our play ground will get answer as 1 1 1 1 1
21st Jul 2018, 1:39 PM
thealoneprogrammer
thealoneprogrammer - avatar
+ 5
problems like this code the main thing is priority and associativity matters as Deepak sir said I have one more code which suits the same problem https://code.sololearn.com/cu7gKp7uNqOX/?ref=app
21st Jul 2018, 1:29 PM
thealoneprogrammer
thealoneprogrammer - avatar
+ 4
++i is pre increment and it gets executed first.. which makes i as 2. I** is post increment and it has second priority after pre increment.. i and *p is same... so (*p)++ is 2 only and then value updates to 3. so output is 322
21st Jul 2018, 12:48 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 4
Anas Sanba int *p is a pointer variable. Its assigning the value of i variables memory address - int *p = &i; Revise the tutorial. Or, go to https://youtube.com/user/thenewboston Search for pointers.
21st Jul 2018, 12:49 PM
Akib
Akib - avatar
+ 4
Guys maybe i am not been clear.... Order evalution of expression passed like function parameters ARE NOT DEFINED by standards then ANY compiler can evalutate they in ANY order.... Here for more simple words https://www.geeksforgeeks.org/g-fact-20/ and here from official source https://en.cppreference.com/w/c/language/eval_order
21st Jul 2018, 1:34 PM
KrOW
KrOW - avatar
+ 3
I want make note that evalutation order with side-effect expressions its not specified in standards (or they have undefined bahviour eg. implementation dependents) https://www.geeksforgeeks.org/g-fact-20/ In practice dont assume that an parameter its evalutated before another
21st Jul 2018, 12:53 PM
KrOW
KrOW - avatar
+ 2
Anas Sanba what shady business is going on! Why is the output messed up. I don't get it. Anyway: Pointers points to the memory address of a variable. The & operator is used to indicate the memory.
21st Jul 2018, 12:31 PM
Akib
Akib - avatar
+ 2
Deepak Gautam shouldn't that be 1? for the first one.
21st Jul 2018, 12:51 PM
Akib
Akib - avatar
+ 2
KrOW ,Thank you. I was really confused bout that.
21st Jul 2018, 12:57 PM
Akib
Akib - avatar
+ 2
thank you so much guys i get it now 😙
21st Jul 2018, 12:57 PM
Anas Sanba
+ 2
as p is pointing to i, *p is nothing but value pointed by i
21st Jul 2018, 12:58 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Deepak Gautam
21st Jul 2018, 1:26 PM
Anas Sanba
+ 2
it's juste one?
21st Jul 2018, 1:46 PM
Anas Sanba
+ 2
ah!! there is exchange between p and i ?
21st Jul 2018, 1:52 PM
Anas Sanba
+ 1
explain me int *p =&i;?
21st Jul 2018, 12:47 PM
Anas Sanba
+ 1
how *p=3?
21st Jul 2018, 12:51 PM
Anas Sanba
0
Ketan so *p =i=1?
21st Jul 2018, 12:53 PM
Anas Sanba
0
I did know about precedence but this worked very weirdly. I am but confused.
21st Jul 2018, 12:55 PM
Akib
Akib - avatar
0
As krow mentioned it's behavior isn't specified in the documentation. I thought arithmetic operations in printf didn't have effect on the variables but in this case it clearly does.
21st Jul 2018, 1:01 PM
Akib
Akib - avatar
0
deepak why value of i become 3?
21st Jul 2018, 1:17 PM
Anas Sanba