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 6 of 11.
Smita said:
1 decade ago
I have confusion with printing values using * and &. Any one please tell me what's the actual meaning of those as well as how we can print values of multiple pointers using * and & ?
Jstin said:
1 decade ago
++p increments it to next address i.e pink.
**p+1.
** refers to p which is double reference(refers to a pointer pointing to another pointer).
**p+1 increments its value by 1(pointer p point to an array of characters s[]), So moves to next element of array 'i'.
**p+1.
** refers to p which is double reference(refers to a pointer pointing to another pointer).
**p+1 increments its value by 1(pointer p point to an array of characters s[]), So moves to next element of array 'i'.
M.sharmila said:
1 decade ago
In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is executed and we get s+1 " 1 = s.
the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is \'ck\'.
the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is \'ck\'.
Venkadesh said:
1 decade ago
I run this program in the turbo C++. But I get the output as " nk " . can anyone tell me why?. The code is,
static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s", **p+1);
static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s", **p+1);
Mayank jain said:
1 decade ago
The second line char **ptr[] = {s+3,s+2,s+1,s};
Reverse the original string whose first element is violet and pointed by *ptr.
Then ++p made it to point pink(second element).
At last **p+1 points to the string from second alphabet of 'pink'.
Reverse the original string whose first element is violet and pointed by *ptr.
Then ++p made it to point pink(second element).
At last **p+1 points to the string from second alphabet of 'pink'.
T.saipavan said:
1 decade ago
Why pointers are to be used?
Prakash said:
1 decade ago
To access the elements efficiently in different methodologies and execution speed is faster.
U Naveena Reddy said:
1 decade ago
What is the use of ** (double star) , *** (triple stars) , if you don't mind, please explain me?
Nani said:
1 decade ago
What is the value of *ptr and ptr in the program? please give me the explanation?
Swati said:
1 decade ago
What does **ptr and ***ptr means? why do we use them? can someone please explain?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers