0 votes 0 votes Suppose the following declarations are in effect : $\text{int }a[ \: ] = \{5,15,34,54,14,2,52,72 \}$; $\text{int }*p = \&a[1] , *q = \&a[5]$; The value of $q - p$ is ________. Programming in C tbb-programming-2 numerical-answers + – Bikram asked May 14, 2017 edited Aug 19, 2019 by Counsellor Bikram 374 views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 5 votes 5 votes sir, here value of q-p should be 4.. if the value of *q - *p had been asked then it should be -13 joshi_nitish answered May 20, 2017 selected Aug 1, 2017 by Bikram joshi_nitish comment Share Follow See all 9 Comments See all 9 9 Comments reply Bikram commented May 21, 2017 reply Follow Share yes, corrected. q is a pointer, it store memory address of 6th element with value 2 . p is another pointer, it store memory address of 2nd element with index 1 whose value is 15 so *q - *p = 2 - 15 = -13 but q-p is 4 as q is index 5 and p is index 1 so q-p = 5-1 = 4 0 votes 0 votes srestha commented Jun 10, 2017 reply Follow Share @Bikram Sir Say integer is taking 4Bytes and start address is 1000 So, q will take 1020 and p will take 1004 So, q-p will be 1020-1004=16 why 4? u havenot divided it by sizeof(int). Am I wrong?? 0 votes 0 votes Bikram commented Jun 10, 2017 reply Follow Share @srestha How you can assume start address is 1000 , it can be any memory location..we can not assume this value . and how you arbitrarily say q will take 1020 and p will take 1004 ? 0 votes 0 votes srestha commented Jun 10, 2017 reply Follow Share I just want to say, if starting address is 1000, then 2nd element starting address will be 1004, and a[5] is 1000+4*5=1020, so, p pointing to 1004 and q pointing to 1020 1 votes 1 votes Bikram commented Jun 11, 2017 reply Follow Share @srestha no, here we consider the value of q - p , here q is the index position and p is the index position . so value for q is 5 and value for p is 1 so q-p is = 5-1 = 4 0 votes 0 votes Learner_jai commented Jun 11, 2017 reply Follow Share @Bikram sir ,please correct me where you found wrong q-p = number of elements by this we can say that between a[5] and a[1] we are having 4 elements but when we are having two add. like int *ptr1,*ptr2; ptr2-ptr1= no of elements present between two address = (address(ptr2)-address(ptr1))/sizeof(data type) 0 votes 0 votes Bikram commented Jun 12, 2017 reply Follow Share @jai q is a pointer, it store memory address of 6th element with value 2 with index 5 . p is another pointer, it store memory address of 2nd element with index 1 whose value is 15. so value for q is 5 and value for p is 1 so q-p is = 5-1 = 4 1 votes 1 votes Anuranjan Chauhan commented Aug 1, 2017 reply Follow Share that is exactly my question 0 votes 0 votes Bikram commented Aug 1, 2017 reply Follow Share @ Anuranjan here we considering the index position of p and q . 0 votes 0 votes Please log in or register to add a comment.