5.1k 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 _______.

| 5.1k views
+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
by Junior (985 points)
selected
+2
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?

+3
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?

explanation

by Active (2.4k points)
+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
by Active (5k points)
+1
a[3] is 40.
+1

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

by Active (2.5k points)