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

Mamatha said:   1 decade ago
*s=s[0],s[1],s[2],s[3] {"black","white","pink","violet"}
s[0]=black, s[1]=white,s[2]=pink,s[3]=violet

**ptr=s+3 means we can write s[3] in the same way
s+2=s[2], s+1=s[1],s=s[0]

++p it can points the adress of the **ptr in this **ptr the first adress of variable is s+3
++p=s+3
after p=s+2
*p=s+2
**p=s[2]=pink
**p+1=ink

Rahul said:   1 decade ago
Single * pointer is used to point simple variable.
whereas, Double ** pointer is used to point another pointer.
*s[] is a pointer and **ptr[] is an another pointer which points to *s[].
for eg.
int *p1,**p2,m;
m=5;
p1=&m;
p2=&p1;
printf("%d",*p1);/*print 5*/
printf("%d",**p2);/*print 5*/

Both the pointers are accessing the same variable.

Ankur Garg said:   1 decade ago
In fifth line which operator is getting execution firstly.

And if it is unary plus operator then how the "p" pointer can remain on element "pink" it should jump on "white".

But if the indirection operator that is * is getting execution first then the ans will be "ink".

But precedence rules say that unary plus operator should be executed first.

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?

Alok said:   6 years ago
int main()
{
char *s[]={"black","white","pink","violet"};
char **ptr[] = {s+1, s, s+3, s+2};
char ***p;
p = ptr; p+1;
printf("%c\n", *(*(*++p+1))+3);
return 0;
}


How this above code is printing 'z', can anyone explain this code in detail?

Aakash said:   1 decade ago
When we use ++p ie we r pre incrementing the address which is stored at p if p is an double pointer ie pointer to pointer
for example:

A knows address of B, and C Knows address of A then in order to
find B, C has to refer to A then thro' A he'll find B
so here A is an single pointer
and C is an double pointer..

Radhakrishna 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

here p=ptr means p,ptr represents same address
++p means ++ptr = pink
++p+1 means ink

Venkadesh said:   1 decade ago
I run this program in the turbo C++. But I get the output as " nk " . can anyone tell me why?. The code is,

static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s", **p+1);

Nagu said:   8 years ago
This program have two error.

int s[]='hold';
*s='C';
print f("%s",s);
error 1:character constent must be one or two character long
error 2:incompatible type conversion

so correct program give below.
char s[]='hold';
*s='C';
print f("%s",s);

The output is = Cold.

Mohan said:   6 years ago
Initially,

p="violet";
++p=>increment p
now p="pink"

When you print
**p+1 means starting from i print the string
if **p+0 means it will print starting from p so "pink" will be printed.
then **p+2 prints "nk"
**p+3 prints "k".
(17)


Post your comments here:

Your comments will be displayed after verification.