Can anyone please tell me how the value of *++ptr becomes 17? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Can anyone please tell me how the value of *++ptr becomes 17?

https://code.sololearn.com/cilZImbd3Lmu/?ref=app

4th Aug 2022, 11:56 AM
Keerthana.S
Keerthana.S - avatar
2 Réponses
+ 2
May I ask first, what did you think happened in line 6 and 9? what are you incrementing? is it value at address of <var> or some other address in memory? Different compiler yields different result.
4th Aug 2022, 3:01 PM
Ipang
+ 1
Keerthana.S the value 17 is whatever happens to be in memory two bytes above the storage for main's local variables. You can see this in a more controlled manner if you define two more local char variables above var. char c = 2; char b = 1; char var=20; char *ptr=&var; *++ptr=2; Now ptr will point to c, and it will no longer print 17. It will print whatever value you use to initialize c (in this example: 2).
5th Aug 2022, 1:50 AM
Brian
Brian - avatar