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.

Mohan said:   6 years ago
I think;

*p= address of p;.
**p=address of string inside ptr.
***p=address of ptr.
++p is also written as ++***p so address of ptr increased.
**p+1 which means **p pointing to "pink" not ptr.
(5)

Travy said:   8 years ago
Please, tell me, why we are using **p+1 in printf? What's the concept?

I know it will print ink but what's the use of using **p instead of p+1?

Here we have set p=ptr, someone please help me out.

Rohit said:   1 decade ago
This is because *--*++p+3 get equivalent to
++p is now pointing to s+1 , *(s+1) is pointing to white,--*(s+1) is pointing to black,*--*(s+1) is Black and ,*--*(s+1)+1 is ck. So Its get printed.

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.

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 & ?

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.

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?

Sudarshan said:   1 decade ago
s is a character pointer.
s[0] = black and ptr[0] = violet.
s[1] = white ptr[1] = pink.
s[2] = pink ptr[2] = white.
s[3] = violet ptr[3] = black.

Prithvi Shankar said:   1 decade ago
I still don't understand the difference between ** and * and ***. How do we say that by having ** the characters of the array are accessed?

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.


Post your comments here:

Your comments will be displayed after verification.