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 10 of 11.
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.
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.
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)
Shreeshail said:
7 years ago
Not getting. Please Help me.
Bhuvi said:
7 years 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 pointing 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 pointing to "ink").
And so is the answer.
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?
{
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?
AKSHAY KALRA said:
6 years ago
@Alok.
s is an array of character pointer.
s[0] = "black" s[1] = "white" s[2] = "pink" s[3] = "violet"
ptr is an array of pointer to character pointer.
ptr[0] = s+1 ptr[1] = s ptr[2] = s+3 ptr[3] = s+2
p is a pointer to pointer to character pointer.
p = ptr
Now try to understand this
*(*(*++p+1))+3)
first ++p will execute and now p = ptr+1
And *p = ptr[1]
*(*(ptr[1]+1))+3)
*(*(s+1))+3)
*(s[1])+3)
*("white")+3
'w'+3
'z'
s is an array of character pointer.
s[0] = "black" s[1] = "white" s[2] = "pink" s[3] = "violet"
ptr is an array of pointer to character pointer.
ptr[0] = s+1 ptr[1] = s ptr[2] = s+3 ptr[3] = s+2
p is a pointer to pointer to character pointer.
p = ptr
Now try to understand this
*(*(*++p+1))+3)
first ++p will execute and now p = ptr+1
And *p = ptr[1]
*(*(ptr[1]+1))+3)
*(*(s+1))+3)
*(s[1])+3)
*("white")+3
'w'+3
'z'
Sriram said:
6 years ago
What is the meaning of ***p, **p, **ptr?
Please anyone explain.
Please anyone explain.
(1)
Sasi said:
6 years ago
Thank you @Adeel.
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".
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers