Reputation: 11948
I went to an interview. Interviewer gave this question.
After seeing this question, I told him that there will be an error as p
is undeclared. But when I ran the program in my PC, I was amazed with the answer/output as ink
that too without any error. Please help me understanding this problem.
#include <iostream>
using namespace std;
int main()
{
static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;//Here
p = ptr;
++p;
cout << **p+1;
return 0;
}
Upvotes: 2
Views: 112
Reputation: 47784
How about this :-
static char *s[] = {"black", "white", "pink", "violet"};
^ ^ ^ ^
| | | |
| | | |
| | | |
+------------+ | |
+---|---|----+ |
| | | |
+ ---|---|---|------------+
| | | |
char **ptr[] = {s+3, s+2, s+1,s};
^ ^
char ***p; | |
| |
p=ptr ; --------+ |
|
++p; -----------------+ {'p','i','n','k'}
^
**p+1 -------------------------+
Upvotes: 8
Reputation: 122373
Since you are only asking about the declaration of p
:
char **ptr[] = {s+3, s+2, s+1, s}, ***p;//Here
is the same as:
char **ptr[] = {s+3, s+2, s+1, s};
char ***p;
Upvotes: 6