+ 1
Void main() { int a[]={1,2,9,8,6,3,5,7,8,9}; int *p = a+1; int *q = a+6; printf("\n%d",q-p); }
what is the output, and how do you solve it.. .more explanation
2 Respuestas
+ 4
First of all understand this,
a[0]=1
a[1]=2
a[3]=9
.
.
.
a[9]=9
So, base address + source index=offset
Therefore, a+1=a[1]
a+6= a[6]
Hence pointer p has the address of a[1]
Similarly, pointer q has the address of a[6]
Assume mathematically,
q-p= (a+6)-(a+1)
= a+6-a-1 // here a cancels out to 0
=6-1
=5
And that same mathematical logic can be explained in terms of pointers as well,when two pointers are subtracted in case of array where pointer points to some element or different array element of the same type array,then the result would be the difference of subscripts only (i.e) (a+ subscript 1 )- (a + subscript 2) as shown above and mathematically demonstrated.
H ence the output prints 5.
So
OUPUT:- 5
you will get output 5.
Check out for more details to pointers in the given below post
https://www.sololearn.com/discuss/1135375/?ref=app
+ 1
yeah! i need more explanation on how it is solved not only the answer.