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;
}
Discussion:
110 comments Page 2 of 11.
Meena said:
4 years ago
Thanks everyone for making it clear.
(2)
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"
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)
Chakshu arora said:
8 years ago
Let starting address for black=1000, white=2000, pink=3000, violet=4000
then s will be a array of [1000, 2000, 3000, 4000] and let its starting address to 5000 which will contain 1000, next 5004 which will contain 2000 and so on.
ptr will be array of address stored in s as [5012(s+3= 5000+3*4(size of pointer)), 5008, 5004, 5000] and let its starting address be [6000,6004,6008,6012]
now p = 6000
++p= 6004
**p+1= **6004+1 =*5008+1 = 3000+1 =3001( as it array of characters)
3001=i (in pink).
%s will start printing from i untill null occurs.
then s will be a array of [1000, 2000, 3000, 4000] and let its starting address to 5000 which will contain 1000, next 5004 which will contain 2000 and so on.
ptr will be array of address stored in s as [5012(s+3= 5000+3*4(size of pointer)), 5008, 5004, 5000] and let its starting address be [6000,6004,6008,6012]
now p = 6000
++p= 6004
**p+1= **6004+1 =*5008+1 = 3000+1 =3001( as it array of characters)
3001=i (in pink).
%s will start printing from i untill null occurs.
(1)
Subhopriyo Dutta said:
7 years ago
*s->'black' 'white' 'pink' 'violet'
s-> 100 101 102 103
*ptr->103 102 101 100
ptr->500 501 502 503
p=ptr=500
++p=501
*(*p)=*(102)->'pink'
printf("%s",**p+1)=ink.
s-> 100 101 102 103
*ptr->103 102 101 100
ptr->500 501 502 503
p=ptr=500
++p=501
*(*p)=*(102)->'pink'
printf("%s",**p+1)=ink.
(1)
Sriram said:
6 years ago
What is the meaning of ***p, **p, **ptr?
Please anyone explain.
Please anyone explain.
(1)
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.
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
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.
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.
A waste fellow said:
1 decade ago
thank u everyone
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers