C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 1)
1.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    ++p;
    printf("%s", **p+1);
    return 0;
}
ink
ack
ite
let
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
110 comments Page 10 of 11.

Divya said:   1 decade ago
@saravanan
***pointer points to third string in the array
whereas ***pointer+1 indicates the third string from 2nd letter

Saravanan said:   1 decade ago
Any body can help me, where we use *** pointer?

Rohit said:   1 decade ago
This is because *--*++p+3 get equivalent to
++p is now pointing to s+1 , *(s+1) is pointing to white,--*(s+1) is pointing to black,*--*(s+1) is Black and ,*--*(s+1)+1 is ck. So Its get printed.

T.Mohan said:   1 decade ago
@Vishal

**p+1 points to the character after p(first character).

Similarly **p+3 points to the character after a(third character).

I hope this may be the answer you expected.

Vishal said:   1 decade ago
If we will take *--*++p+3 in place of **p+1 then ck from black is printed as o/p but why ?

Any one can tell me dis.

Vishal said:   1 decade ago
In this if we will not take static char then nothing will happen to o/p.

Vinoth said:   1 decade ago
What is the use of static keyword in this program

Rahul said:   1 decade ago
Single * pointer is used to point simple variable.
whereas, Double ** pointer is used to point another pointer.
*s[] is a pointer and **ptr[] is an another pointer which points to *s[].
for eg.
int *p1,**p2,m;
m=5;
p1=&m;
p2=&p1;
printf("%d",*p1);/*print 5*/
printf("%d",**p2);/*print 5*/

Both the pointers are accessing the same variable.

Rohan said:   1 decade ago
Can any one explain me when to use single * and double ** in pointer?

Soujanya ,ravali said:   1 decade ago
If in the above program instead of **p+1 if it is *--*++p+3 is given

What is the output? Give explanation.


Post your comments here:

Your comments will be displayed after verification.