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 3 of 11.

Vamshi said:   1 decade ago
1. s is an array of 4 colors where s having starting address of black, s+1 having starting address of white and so on.

2. ptr is an array of the starting addresses and p is pointer to ptr array.

3. ++p means at p+1 location s+2 is stored. **p means pink and **p+1 means address of alphabet "i" in pink. then it prints the string from i to end of the string.

Ankur Garg said:   1 decade ago
In fifth line which operator is getting execution firstly.

And if it is unary plus operator then how the "p" pointer can remain on element "pink" it should jump on "white".

But if the indirection operator that is * is getting execution first then the ans will be "ink".

But precedence rules say that unary plus operator should be executed first.

Saurav said:   1 decade ago
Can anybody tell me when to use single * and when to use double and triple *?

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.

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

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.

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

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

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.

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.


Post your comments here:

Your comments will be displayed after verification.