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

Aki said:   9 years ago
It's so useful.

AKSHAY KALRA said:   9 years ago
static char *s[] = {"black", "white", "pink", "violet"};

It means --> s[0] = "black" s[1] = "white" s[2] = "pink" s[3] = "violet"

char **ptr[] = {s+3, s+2, s+1, s}, ***p;

It means --> ptr[0] = s+3 ptr[1] = s+2 ptr[2] = s+1 ptr[3] = s

And, p = ptr --> &(s+3)

Now ++p;

That means p = p+1 --> ptr+1 --> &(s+2)

Now *p ---> s+3
**p ---> *(s+3) --> s[3] --> "pink"
**p+1 --> "ink".

Situ said:   9 years ago
If we change position of color like.

{white, pink, violet, black} and,
char **ptr[] = {s, s+1, s+2,s+3}, ***p;

And all the process remain same then what will be the output?

Please answer me.

Akshay Kalra said:   9 years ago
@Situ Answer would remain same.

Now in your case:-

static char *s[] = {"white", "pink", "violet", "black"};

It means --> s[0] = "white" s[1] = "pink" s[2] = "violet" s[3] = "black"

char **ptr[] = {s, s+1, s+2, s+3}, ***p;

It means --> ptr[0] = s ptr[1] = s+1 ptr[2] = s+2 ptr[3] = s+3

And, p = ptr --> &s

Now ++p;

That means p = p+1 --> ptr+1 --> &(s+1)

Now *p ---> s+1
**p ---> *(s+1) --> s[1] --> "pink"
**p+1 --> "ink".

Jagruti said:   9 years ago
**p+1 why that is used why **?

Shreyas said:   9 years ago
Thanks @Adeel. It seemed simple and easy :).

Uma said:   9 years ago
I am not understanding where we use the ***p anyone help me.

Ramya said:   9 years ago
Are we not supposed to use *** to access "pink" by p?

Isn't it should be ***p+1?

Priya said:   9 years ago
I don't understanding the concept of ***p. Please explain me.

LOÏS said:   9 years ago
When one write char ***p; p is adress of an array of char;
i.e:
char *s[]={"white", "pink", "violet", "black"}; // s is an array of char who is equal to char **s.

If you want a pointer to point on that array you will declare it as char ***ptr=&s and then:
ptr--->&s;--->@@white
*ptr---->s;--->@white
**ptr---->*s--->white
**ptr+1--->*s+1--->hite

Now look at Akshay Kalra concept, it's very clear.


Post your comments here:

Your comments will be displayed after verification.