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

Nagesh said:   1 decade ago
@Manali,

See below:
*K - Value at K.
**K - Value at *K.
**K - Value at **K.

Still confusing dear ?

Piyush prakash said:   1 decade ago
b l a c k
1000 1001 1002 1003 1004

w h i t e
2000 2001 2002 2003 2004

p i n k
3000 3001 3002 3003

v i o l e t
4000 4001 4002 4003 4004 4005

Since all the above are characters so only 1 byte difference in address. I have given all characters an address
now,

*s means going to this address there is value.
**s means going to this address there is another address and going to that address there is value..and so on.
***s...
****s...

s contains all starting address:

1000 2000 3000 4000-s
5000 5004 5008 5012 (I have taken any arbitrary address).

You can read the above as5000 address is allocated as starting address to s and since pointer takes 4 bytes so 5004 is assigned to 2000 and so on..

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

This means ptr is an array having elements s+3,s+2,s+1,s
s+3 means s=5000 (by default) and skipping 3 elements so skipp 5000, 5004, 5008 and so s+3=5012.

The elements in ptr will be like this:

5012 5008 5004 5000-ptr.
6000 6004 6008 6012(I have taken any arbitrary address).
Read as I have explained earlier.

***p;
p = ptr;
++p;
printf("%s", **p+1);

So p will contain 6000.
++p means 6000 will get replaced by 6004.
**p+1 means p=6004 going to 6004 address we will get 5008 as address and going to that address we will get 3000 as address and going to that address we will get 'p'. Since its +1 after **p so skip 1 element ink will be the string.

Thanks.

ABC said:   1 decade ago
Please tell me how can we analyze char **ptr[] is reverse of char *s[].

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.

Baby Noon said:   1 decade ago
Actually I am not getting this. Could anyone explain me this in an easy manner?

Shobhna raj said:   1 decade ago
#include<stdio.h>
int main()
{
static char *s[] = {"black", "white", "yellow", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;

printf("%s",*--*++p+3);
return 0;
}

The output is "te". Can anyone explain how is printf function working here?

Venkatanarayana pamidi said:   1 decade ago
This is a 2D array: static char*s[] = {"black", "white", "pink", "violet"};

This is an array of char**: char**ptr[] = {s+3, s+2, s+1, s} with first element at "violet".

p = ptr;

This will increment the pointer, in this case it will increment by 1 to the next array element i.e. pink: ++p;

This one has operator precedence also mixed with ++, so, it is actually (**p) + 1, remember all strings are NULL terminated, so it will print "pink" from "i" or "ink: printf ("%s", **p+1);

Shalu said:   1 decade ago
I want answer of printf("%s, *--*++p+3).

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?

Binod said:   1 decade ago
I don't understand ***p step.


Post your comments here:

Your comments will be displayed after verification.