search
Log In
37 votes
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 _______.

in Programming 6.8k views
4
Notice-> In proper answer there is no space between 1 and 40.
0
Please answer the following question with the help of diagram

*(*(a+3)+2)?
0
Little modification need in best answer.

Address in ptr should use rather than address of ptr?

6 Answers

47 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;

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 by
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 ??
1

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?

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.

it may help you https://gateoverflow.in/282711/madeeasy-programming-language-2019?show=282720#c282720

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 !

24 votes

explanation

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

0 votes

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

Ans: 1, 40

Answer:

Related questions

28 votes
2 answers
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 ______.
asked Feb 16, 2015 in Programming jothee 4.5k views
47 votes
1 answer
2
7.4k views
Consider the following C program: #include<stdio.h> int main() { int i, j, k = 0; j=2 * 3 / 4 + 2.0 / 5 + 8 / 5; k-=--j; for (i=0; i<5; i++) { switch(i+k) { case 1: case 2: printf("\n%d", i+k); case 3: printf("\n%d", i+k); default: printf("\n%d", i+k); } } return 0; } The number of times printf statement is executed is _______.
asked Feb 16, 2015 in Programming jothee 7.4k views
29 votes
5 answers
3
4.1k views
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$
asked Feb 14, 2015 in Programming jothee 4.1k views
12 votes
3 answers
4
2k views
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 ________.
asked Feb 16, 2015 in Numerical Methods jothee 2k views
...