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.
Soujanya ,ravali said:
1 decade ago
If in the above program instead of **p+1 if it is *--*++p+3 is given
What is the output? Give explanation.
What is the output? Give explanation.
Rohan said:
1 decade ago
Can any one explain me when to use single * and double ** in pointer?
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.
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.
Vinoth said:
1 decade ago
What is the use of static keyword in this program
Vishal said:
1 decade ago
In this if we will not take static char then nothing will happen to o/p.
Vishal said:
1 decade ago
If we will take *--*++p+3 in place of **p+1 then ck from black is printed as o/p but why ?
Any one can tell me dis.
Any one can tell me dis.
T.Mohan said:
1 decade ago
@Vishal
**p+1 points to the character after p(first character).
Similarly **p+3 points to the character after a(third character).
I hope this may be the answer you expected.
**p+1 points to the character after p(first character).
Similarly **p+3 points to the character after a(third character).
I hope this may be the answer you expected.
Rohit said:
1 decade ago
This is because *--*++p+3 get equivalent to
++p is now pointing to s+1 , *(s+1) is pointing to white,--*(s+1) is pointing to black,*--*(s+1) is Black and ,*--*(s+1)+1 is ck. So Its get printed.
++p is now pointing to s+1 , *(s+1) is pointing to white,--*(s+1) is pointing to black,*--*(s+1) is Black and ,*--*(s+1)+1 is ck. So Its get printed.
Saravanan said:
1 decade ago
Any body can help me, where we use *** pointer?
Divya said:
1 decade ago
@saravanan
***pointer points to third string in the array
whereas ***pointer+1 indicates the third string from 2nd letter
***pointer points to third string in the array
whereas ***pointer+1 indicates the third string from 2nd letter
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers