The Gateway to Computer Science Excellence
+31 votes

Consider the following C program

int main()  {
    static int a[] = {10, 20, 30, 40, 50};
    static int *p[] = {a, a+3, a+4, a+1, a+2};
    int **ptr = p;
    printf("%d%d", ptr-p, **ptr);  

The output of the program is _______.

in Programming by Veteran (105k points) | 5.1k views
Notice-> In proper answer there is no space between 1 and 40.
Please answer the following question with the help of diagram

Little modification need in best answer.

Address in ptr should use rather than address of ptr?

5 Answers

+43 votes
Best answer
static int a[] = {10, 20, 30, 40, 50};
static int *p[] = {a, a+3, a+4, a+1, a+2};
int **ptr = p;



$\text{ptr-p} = \frac{\text{address of ptr} - \text{address of p}}{\text{sizeof(*ptr))}} = 1$

$\text{**ptr = p[2] = *(a+3) = 40}$

printf("%d%d", ptr-p, **ptr);  // 140
by Junior (985 points)
selected by
when a pointer is subtracted from another pointer it gives the no of memory blocks between them. ptr is P[1] and p is P[0]. hence Pr-p=1

i think **ptr=p[1] = *(a+3) = 40

@salman sir , I think it would be 1,40.  instead of 140 .. m I correct???
@shweta "," is not given in printf. So, it will print 140 and not 1,40
Can anyone recommend me any resource for ptr-p part ?? y r u dividing it by sizeof ??

Sir, please verify

if we need to find ptr+p and type of p is char;

then  ptr+p = $\frac{address(ptr)+address(p)))}{size(*p)}=\frac{5}{1}=5$

Also one more doubt, if the type is int then pointer operations should be divisible by size of variables? otherwise it is invalid?

ptr+p is not allowed in pointer arithmatic.

This is an excerpt from the above link:

p-q //If p and q are pointers p-q will work as above and thus will return the no. of objects of type of p (p and q must be of same data type or else it is compilation error)

I don't understand the statement (will return the number of objects of type of p). What does "type" of p mean? How can I make multiple number of objects of type p such that on doing p-q I get a number other than 1?

nice point arjun sir

@Anil Khatri @Salman @Shaik Masthan Pls explain 

int **ptr = p should store the address of complete array (say 3000)

on incrementing ptr , the value in the ptr should get incremented to 3000+5*2 = 3010 (considering int of 2 bytes)


pls tell me where i am getting wrong or in which case the address of complete array get stored.


first learn the concept of array clearly, then automatically you can understood this question.

it may help you


@Shaik Masthan how the size of the pointer is 2 here? or if it is not then what size of the pointer should be taken?

Why pointer additions is not allowed?
+21 votes


by Active (2.4k points)
good xplanation!
+14 votes
for ptr-p =1, as pointer increment adds the size of the data type and pointer subtraction gives the number of objects that can be held in between the two addresses = diff(addr1, addr2)/ sizeof(data type)

and for **ptr = *(a+3) = a[3] = 40
by Active (5k points)
a[3] is 40.
here answer will be 140..(ptr-p)=(address of incremented ptr -  starting address of ptr)/sizeof(int)=1

and **ptr =40 so 140
+4 votes
Execute this program on any compiler. The output will be 140.
by (341 points)
Inthe question I think it will be *** ptr because p itself is like **p. So if we consider this then *** ptr will be segmentation fault
0 votes

In Layman's terms:

ptr-p = location of the block where ptr points - location of the block where p points.
ptr and p point to neighbouring cells, hence their difference is 1.
You can also see that as subtracting indices, which works in all the cases.
ptr points to index 1, and p points to index 0.


Now, dereference ptr twice.

ptr » a+3 » 40


So, 140.

by Active (2.5k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,645 questions
56,559 answers
101,599 users