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

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.

Xyz said:   9 years ago
What is the output if we put %c instead of %s in printf statement?

Emanuel said:   9 years ago
I think it doesn't work.

The 1st line keeps the array of char pointers. The array of addresses of the first team of each char array, and who will keep the each char array itself?

Correct me, if I am wrong.

Emanuel said:   9 years ago
I think the name of the array is a pointer to the 1st method of the array, but when it is stated implicitly how it can be catched by a pointer to char?


Post your comments here:

Your comments will be displayed after verification.