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 8 of 11.
Venkatanarayana pamidi said:
1 decade ago
This is a 2D array: static char*s[] = {"black", "white", "pink", "violet"};
This is an array of char**: char**ptr[] = {s+3, s+2, s+1, s} with first element at "violet".
p = ptr;
This will increment the pointer, in this case it will increment by 1 to the next array element i.e. pink: ++p;
This one has operator precedence also mixed with ++, so, it is actually (**p) + 1, remember all strings are NULL terminated, so it will print "pink" from "i" or "ink: printf ("%s", **p+1);
This is an array of char**: char**ptr[] = {s+3, s+2, s+1, s} with first element at "violet".
p = ptr;
This will increment the pointer, in this case it will increment by 1 to the next array element i.e. pink: ++p;
This one has operator precedence also mixed with ++, so, it is actually (**p) + 1, remember all strings are NULL terminated, so it will print "pink" from "i" or "ink: printf ("%s", **p+1);
Shalu said:
1 decade ago
I want answer of printf("%s, *--*++p+3).
Prithvi Shankar said:
1 decade ago
I still don't understand the difference between ** and * and ***. How do we say that by having ** the characters of the array are accessed?
Binod said:
1 decade ago
I don't understand ***p step.
Madhav said:
1 decade ago
What is the use of static in the program?
Digvijay said:
1 decade ago
Thank you all for great explanation.
Murthy said:
1 decade ago
s is a character pointer.
s[]:
s[0]=black.
s[1]=white.
s[2]=pink.
s[3]=violet.
ptr[]:
ptr[0]=violet.
ptr[1]=pink.
ptr[2]=white.
ptr[3]=black.
Here p=ptr i.e p, ptr are the same address.
++p is equal to ++ptr that means pink. For these p++ ia added the +1 that means ++p+1 that is **p+1 means ink.
s[]:
s[0]=black.
s[1]=white.
s[2]=pink.
s[3]=violet.
ptr[]:
ptr[0]=violet.
ptr[1]=pink.
ptr[2]=white.
ptr[3]=black.
Here p=ptr i.e p, ptr are the same address.
++p is equal to ++ptr that means pink. For these p++ ia added the +1 that means ++p+1 that is **p+1 means ink.
Bhas said:
1 decade ago
How **p+1 means ink?
Bhavya kp said:
1 decade ago
It refers second variable. And also gives output from second word of referred variable.
Dipali said:
1 decade ago
I can't understand. Please clrealy discuss.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers