How does this output comes? I ask this question,because i found this question in C challenges... | SoloLearn: Learn to code for FREE!

+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

10/21/2020 2:05:39 PM

Yogeshwaran

15 Answers

New Answer

+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.

+5

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

+4

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

+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.

+3

Yogeshwaran Have a look at this: https://code.sololearn.com/cg0dY8X4JP5u/?ref=app

+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

+2

Thank you Davide 😊

+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.....😊

+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.....

+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

+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......

+1

Davide and Programmer Raja (தமிழன்) See these code also...... Please explain it's behaviour... https://code.sololearn.com/cv0pLwwdjl8K/?ref=app https://code.sololearn.com/c9pBby9vD3oI/?ref=app

+1

Yes Programmer Raja (தமிழன்) bro....😁😅. But, Please explain my comment bro... By relating those two codes....

+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 ?

+1

Yogeshwaran you're welcome! 😄