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 11 of 11.
Kavya said:
8 years ago
Good explanation, thank you @Adeel.
Swati said:
8 years ago
Thank you @Adeel.
Anitha said:
8 years ago
find the output
int s[]='hold';
*s='C';
print f("%s",s);
Can anyone please answer this question?
int s[]='hold';
*s='C';
print f("%s",s);
Can anyone please answer this question?
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.
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'
Sasi said:
6 years ago
Thank you @Adeel.
Uhfgbh said:
4 months ago
Then where do we use *** pointer?
Anyone, please explain to me.
Anyone, please explain to me.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers