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

XYZ said:   2 decades ago
1st line of the program makes an array of char pointers(i.e strings in c++), having s[0] = "black" ... etc.

2nd line create in another array having reversed order of strings in array 's'(i.e. the first string in this array is "voilet").

3rd line making **p to point to the same address as **ptr is pointing to.

4th line increases the address of by one to which the **p is pointing to(i.e now **p is pointing to string "pink").

5th line dereferences the next address within the string which was pointing by **p(i.e firstly **p is now poiting to "ink").

And so is the answer.

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

Kriti jain said:   1 decade ago
char **ptr[]=***p;
p=ptr;
sp p containg the address of p , so p contains the initial value address that is s+3;
now ++p;
so p is increase by one .
hence p contains address of s+2 ;
and **p define the value at s+2 ;
which is pink
so in **p+1 is ink.

Manohar said:   1 decade ago
Can somebody give clear explanation please.

RaghuShri said:   1 decade ago
First line :*s[]={-------} defines a pointer to an array of those listed colors

Second line: **ptr[]={-----} defines a pointer to an array of colors in reverse order as defined by s[]

Third line:now pointer 'p' refers to the same address of ptr pointing to, now ptr is starting with address of "pink" so p is starting at "p" in pink

Fourth line:increasing the address of pointer 'p' by 1 that is now pointer 'p' is pointing at "i"

Fifth line" p+1 means to print the first value stored in pointer ptr ie pink, but starting from pointer 'p' so it will print "ink"
(1)

A waste fellow said:   1 decade ago
thank u everyone

Vamshi said:   1 decade ago
1. s is an array of 4 colors where s having starting address of black, s+1 having starting address of white and so on.

2. ptr is an array of the starting addresses and p is pointer to ptr array.

3. ++p means at p+1 location s+2 is stored. **p means pink and **p+1 means address of alphabet "i" in pink. then it prints the string from i to end of the string.

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.

Adeel said:   1 decade ago
ptr = {pointer to "violet", pointer to "pink", pointer to "white", pointer to "black"}

p = ptr --> *p = pointer to "violet"

++p --> *p = pointer to "pink"

This implies that:

*p = {'p','i','n','k','\0'}

Which means:

**p = 'p'
**p + 1 = 'i'

so **p + 1 is a pointer to this string: {'i', 'n', 'k', '\0'}, which is simply "ink"
(3)

Saurav said:   1 decade ago
Can anybody tell me when to use single * and when to use double and triple *?


Post your comments here:

Your comments will be displayed after verification.