1,574 views

5 Answers

Best answer
5 votes
5 votes
int *p = &a[1] , *q = &a[5] ;
    return q-p;

q-p is nothing but no of elements (of type (*q) or (*p) which is int here) in between q and p. which is 4.

selected by
5 votes
5 votes

Assuming size of integer = 2 bytes and base address = 1000.

It can be interpreted as:

Element Access Address
10 a 1000
20 a+1 1002
30 a+2 1004
40 a+3 1006
50 a+4 1008
60 a+5 1010

Then

p = a + 1 (or 1002)

q = a + 5 (or 1010)

Hence, q - p = (a + 5) - (a + 1) = 4 (or (1010-1002)/2 = 4)

0 votes
0 votes
p and q both are pointer variable p hold the address of (a+1) means address of  a[1] . q will  hold the address of (a+5) means &a[5].

let suppose base address of a is 100  and every integer will take 2 byte so &a[1]=102 and &a[5]=110.

so 110=102=8.

value will  return 8.
0 votes
0 votes

For simplicity, say each element takes 2B, and assume base address is 100.

element at address 100 = 10

element at address 102 = 20

element at address 104 = 30

element at address 106 = 40

element at address 108 = 50

element at address 110 = 60

 

NOTE: Confusion is commonplace here. 

p points to 102, and q points to 110. BUT *p is 20 and *q is 60; which is NOT asked here.

 

q-p would be 110 - 102 = 8.

But each element is of 2B, so scale it down by a factor of 2.

=> 8/2

=> 4


You can simply think of it as how many places is q far apart from p.

Answer:

Related questions

3 votes
3 votes
4 answers
1
Arjun asked Oct 18, 2016
775 views
What will be the output of the following code?#include <stdio.h int main() { char a = 'A', z = 'Z'; printf("%d", z-a); }
12 votes
12 votes
1 answer
2
3 votes
3 votes
2 answers
3
7 votes
7 votes
3 answers
4
Arjun asked Oct 18, 2016
1,592 views
The output of the following C program will be _____#include<stdio.h #define type int type foo(type b) { return b*b; } #undef type #define type float int main() { float a ...