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

Lakshmi said:   1 decade ago
@suchismita.
We can print white as:
printf("%s",++s);
or
Give *s as a pointer to any varibale name, you can indirectly do so
printf("%s",s+1);
Here
printf("%s",*s+1);

Prashant said:   1 decade ago
If I write *(*p+1) why violet is printed ?

Beauty of the nation said:   1 decade ago
Please tell me how can we analyze this description.

Nani said:   1 decade ago
Can anyone please tell me why is ++p equal to pink here?

RAHUL KUMAR said:   1 decade ago
In first line there are four character arrays

s+3 means s[3] means voilet
s+2.......etc

**p+1 will first point to s2 and in that string control will move one
String forwad and print that
i.e s[3]=pink
**p+1=ink

Sudhakar said:   1 decade ago
In char **ptr[] declaration why do we use double *
Even though output will be same for single *

Jassi said:   1 decade ago
Why we declared p as triple pointer. And then equated it with ptr i.e. ptr=p;.

Smita said:   1 decade ago
I have confusion with printing values using * and &. Any one please tell me what's the actual meaning of those as well as how we can print values of multiple pointers using * and & ?

Jstin said:   1 decade ago
++p increments it to next address i.e pink.

**p+1.

** refers to p which is double reference(refers to a pointer pointing to another pointer).

**p+1 increments its value by 1(pointer p point to an array of characters s[]), So moves to next element of array 'i'.

M.sharmila said:   1 decade ago
In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is executed and we get s+1 " 1 = s.

the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is \'ck\'.


Post your comments here:

Your comments will be displayed after verification.