in Programming in C
501 views
0 votes
0 votes
when we have two pointers p,q pointing to same array to find the number of elements we should give  q-p or q-p+1
in Programming in C
501 views

2 Answers

1 vote
1 vote

To find the number of elements it should be q - p +1.

         

        200                      ( p) 202                 204                (q) 206                     208

q-p =(206-202)/2=2

no of elements=2+1=3

1 comment

That's correct.
However, the question is a bit ambiguous. It should be "...to find the number of elements between p and q (inclusive)".

0
0
0 votes
0 votes

let A[5] is an array of integers {4 byte each element} with base address = 1000

Now suppose , p =1004, and q = 1012

q-p = 8 , now divide it by size of element i.e 8/4 =2 {it is difference between p and q in terms of elements}

4 Comments

So According to you to find the number of elements we should use q-p rather than q-p+1
0
0

 saipriyab

if p is holding address of first element and q is holding address of last element the address of last element then:-  q-p will give no. of block difference b/w p and q and that would be (n-1)....therefor for finding the size we would use ...q-p+1

0
0

@Akash Mittal

i think difference of pointer need not to be devided by size of data...bcoz  diffrence of pointer return no. of block b/w them...??

0
0
@hs_yadav

we need to divide the difference of pointers by the size if we want to know the number of elements between them.

got it q-p+1 gives total number of elements in the array
0
0

Related questions