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

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
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".

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

Divya said:   10 years ago
printf("%s", **p+1);

Here why we use double pointer in **p+1.

What will happen, if we use single pointer like *p+1.

Dipali said:   10 years ago
I can't understand. Please clrealy discuss.

Bhavya kp said:   10 years ago
It refers second variable. And also gives output from second word of referred variable.

Bhas said:   1 decade ago
How **p+1 means ink?

Murthy said:   1 decade ago
s is a character pointer.

s[]:

s[0]=black.
s[1]=white.
s[2]=pink.
s[3]=violet.

ptr[]:

ptr[0]=violet.
ptr[1]=pink.
ptr[2]=white.
ptr[3]=black.

Here p=ptr i.e p, ptr are the same address.

++p is equal to ++ptr that means pink. For these p++ ia added the +1 that means ++p+1 that is **p+1 means ink.

Digvijay said:   1 decade ago
Thank you all for great explanation.

Madhav said:   1 decade ago
What is the use of static in the program?


Post your comments here:

Your comments will be displayed after verification.