- 3
Check this program and predict your outputs.
Please Explain the program execution ... https://code.sololearn.com/cKFw6Sv2TE3x/?ref=app
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
+ 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)
+ 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 " ]
+ 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;
}
+ 1
I assume that &arr[0]=0 so &arr[4]=4 hence the difference is 4 because array allocates in contiguous manner.
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;
}
- 1
Akash Singh
how you calculated &arr[4]=4 and &arr[0]=0 ?