+ 7

How does this output comes? I ask this question,because i found this question in C challenges...

Please help me to find,How does this output comes....... In these below codes......... i.e int r = p - q; // output You can also see that question in my post https://www.sololearn.com/post/691377/?ref=app https://code.sololearn.com/cxFx019iI7TW/?ref=app https://code.sololearn.com/c9pBby9vD3oI/?ref=app

21st Oct 2020, 2:05 PM
Yogeshwaran P
Yogeshwaran P - avatar
15 Réponses
+ 5
In the first code if you had done r = a[0] - a[1] The program would have printed r = 1 - 2 I.e. -1 But you've done r = p - q So the program print the difference in sizeof(int) bytes (because they are arrays of int) between the position in memory of p and that of q. Since array elements are by definition stored in contiguous memory location, 'locationOfSubscriptX' - 'locationOfSubscriptX+1' is by definition -1 In the second code the same thing is happening. You are subtracting the memory position of the pointers. Since the result is 1, we know that, in the memory, a is stored right after b, I don't know why.
21st Oct 2020, 2:06 PM
Davide
Davide - avatar
21st Oct 2020, 3:49 PM
Programmer Raja
Programmer Raja - avatar
+ 4
https://code.sololearn.com/cLgixF4Ryq46/?ref=app
21st Oct 2020, 3:48 PM
Programmer Raja
Programmer Raja - avatar
+ 3
Yogeshwaran look at the hexadecimal addresses of the p and q in the second code. I think we see different addresses, but the concept is the same. For me they are exactly the same except for the last character: for a is 8 and for b is 4. So when you do exaexaexa8 - exaexaexa4 you got 4. But since they are int and they are stored in 4 byte (in this case), 4 is a unit. So you got 1. If you change all the "int" types with "char" you'll see that the two addresses have a difference of 1 and not 4. Because char are stored in 1 byte. Again I don't know why a is stored right after b. But that's another story.
21st Oct 2020, 3:20 PM
Davide
Davide - avatar
21st Oct 2020, 4:06 PM
Davide
Davide - avatar
+ 3
Yogeshwaran Thank you Programmer Raja (தமிழன்) bro for yours effort in solving my doubt😊 can you tell me why these address size are changing...... while simply interchanging the variable initialisation..... i.e &a > &b in one code and &a < &b in another code ? I think it depend on compiler
22nd Oct 2020, 12:15 AM
Programmer Raja
Programmer Raja - avatar
+ 2
Thank you Davide 😊
21st Oct 2020, 4:29 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
Thank you so much Davide and Programmer Raja (தமிழன்) bro..... For clearing my doubt....... Now I only confused.which answer can be marked as a "best answer"😅 Because both answers are solved my doubt.....😊
22nd Oct 2020, 7:12 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Thank you Davide 😊.It make me to understand about pointer difference (subtraction in array)......... In Code 1..... But I don't understand clearly how does output comes in code 2....... Please answer related to code 2...... If any one know ,please answer.....
21st Oct 2020, 2:45 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Bro it print one because the size of 1 integer is 4 bit 1 int =4bit Let's have example a=12(stored in address 30) b=20(stored in address 34) So the difference is 4bit which is equals to 1 integer 4bit =1 int
21st Oct 2020, 3:15 PM
Programmer Raja
Programmer Raja - avatar
+ 1
Thank you so much Programmer Raja (தமிழன்) bro😊 But &a - &b will give [ - 1 ] only bro from yours answer 😅 i.e &a - &b = address(30 - 34 ) = -4 bit[-1 int] // am I right? Then why Output comes as '1' 😅 It means that compiler made address of a is 34 && address of b is 30.... Or some other reason.....? Please tell me bro......
21st Oct 2020, 3:21 PM
Yogeshwaran P
Yogeshwaran P - avatar
21st Oct 2020, 3:36 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Yes Programmer Raja (தமிழன்) bro....😁😅. But, Please explain my comment bro... By relating those two codes....
21st Oct 2020, 3:38 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Thank you Programmer Raja (தமிழன்) bro for yours effort in solving my doubt😊 can you tell me why these address size are changing...... while simply interchanging the variable initialisation..... i.e &a > &b in one code and &a < &b in another code ?
21st Oct 2020, 3:54 PM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Yogeshwaran you're welcome! 😄
21st Oct 2020, 4:31 PM
Davide
Davide - avatar