# GATE2015-3-26

6.8k views

Consider the following C program

#include<stdio.h>
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;
ptr++;
printf("%d%d", ptr-p, **ptr);

}  

The output of the program is _______.

4
Notice-> In proper answer there is no space between 1 and 40.
0

*(*(a+3)+2)?
0
Little modification need in 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;

ptr++;




$\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

selected
4
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
0

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

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

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?

5
ptr+p is not allowed in pointer arithmatic.
2

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?

0
nice point arjun sir
0

@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.

1

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

0

@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?

0
Why pointer additions is not allowed?
0

What if there is

ptr--

In place of

ptr++

Thank you !

explanation

1
good xplanation!
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
1
a[3] is 40.
1

and **ptr =40 so 140
Execute this program on any compiler. The output will be 140.
0
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

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.

Note: We are assuming the size of Pointer is 2byte that's why (Content of Ptr - Content of p)/2

## Related questions

1
4.5k views
Consider the following C program: #include<stdio.h> int f1(void); int f2(void); int f3(void); int x=10; int main() { int x=1; x += f1() + f2 () + f3() + f2(); printf("%d", x); return 0; } int f1() { int x = 25; x++; return x;} int f2() { static int x = 50; x++; return x;} int f3() { x *= 10; return x;} The output of the program is ______.
Consider the following C program segment. # include <stdio.h> int main() { char s1[7] = "1234", *p; p = s1 + 2; *p = '0'; printf("%s", s1); } What will be printed by the program? $12$ $120400$ $1204$ $1034$
The velocity $v$ (in kilometer/minute) of a motorbike which starts form rest, is given at fixed intervals of time $t$ (in minutes) as follows: t 2 4 6 8 10 12 14 16 18 20 v 10 18 25 29 32 20 11 5 2 0 The approximate distance (in kilometers) rounded to two places of decimals covered in 20 minutes using Simpson's $1/3^{rd}$ rule is ________.