- 3

Check this program and predict your outputs.

Please Explain the program execution ... https://code.sololearn.com/cKFw6Sv2TE3x/?ref=app

11th May 2021, 12:24 AM
Amaresh Ray
Amaresh Ray - avatar
7 odpowiedzi
+ 2
Your program has some syntax and logical errors. After removing this errors i get the output: 4 Because &arr[4]=4 and &arr[0]=0 so simply 4-0=4
11th May 2021, 12:42 AM
Akash Singh
Akash Singh - avatar
+ 2
Amaresh Ray I think the size of integer in the given compiler is 4, that's why you got the difference of 16 between the addresses of 1st and 5th element.(Each element takes 4 which acts as space of an element and defined by same address , because one element is defined by a single address not more than one)
11th May 2021, 10:34 AM
Akash Singh
Akash Singh - avatar
+ 1
hey Amaresh Ray you get answer " 4 " because you are using pointers arithmetic ! if you want output in simple or reak byte difference then , 1. multiply by size of data type to difference of addresses (result) 2. or simply multiply by " 4 " in your case , (because of integer array) [remove one extra curly braces above the return and use long int " %ld " ]
11th May 2021, 12:57 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar
+ 1
There is one extra curly braces in your program use ld instead of %d It will work without warnings #include <stdio.h> int main() { int arr[5]={10,20,30,40,50}; printf("%ld",&arr[4]-&arr[0]); return 0; }
11th May 2021, 3:03 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
I assume that &arr[0]=0 so &arr[4]=4 hence the difference is 4 because array allocates in contiguous manner.
11th May 2021, 3:59 AM
Akash Singh
Akash Singh - avatar
0
Hey Akash Singh Your answers is correct But still I have some doubt regarding this bcz I am getting value of &arr[4] and &arr[0] is something which has difference of 16. I'm trying to find this way:--> #include <stdio.h> int main(){ int arr[5]={10,20,30,40,50}; printf("%ld %ld\n",&arr[4], &arr[0]); printf("%ld",&arr[4]-&arr[0]); return 0; }
11th May 2021, 8:52 AM
Amaresh Ray
Amaresh Ray - avatar
- 1
Akash Singh how you calculated &arr[4]=4 and &arr[0]=0 ?
11th May 2021, 3:21 AM
Giriraj Yalpalwar
Giriraj Yalpalwar - avatar