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 4 of 11.
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'.
Amit Wadhe said:
1 decade ago
**ptr[] = {"violet, "pink", "white", "black"};
p = ptr;
++p; // So now, p points to "pink"
printf("%s", **p+1); // This will increment pointer within pink, so it will be "ink"
Output: ink
p = ptr;
++p; // So now, p points to "pink"
printf("%s", **p+1); // This will increment pointer within pink, so it will be "ink"
Output: ink
Kriti jain said:
1 decade ago
char **ptr[]=***p;
p=ptr;
sp p containg the address of p , so p contains the initial value address that is s+3;
now ++p;
so p is increase by one .
hence p contains address of s+2 ;
and **p define the value at s+2 ;
which is pink
so in **p+1 is ink.
p=ptr;
sp p containg the address of p , so p contains the initial value address that is s+3;
now ++p;
so p is increase by one .
hence p contains address of s+2 ;
and **p define the value at s+2 ;
which is pink
so in **p+1 is ink.
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'.
Subhopriyo Dutta said:
7 years ago
*s->'black' 'white' 'pink' 'violet'
s-> 100 101 102 103
*ptr->103 102 101 100
ptr->500 501 502 503
p=ptr=500
++p=501
*(*p)=*(102)->'pink'
printf("%s",**p+1)=ink.
s-> 100 101 102 103
*ptr->103 102 101 100
ptr->500 501 502 503
p=ptr=500
++p=501
*(*p)=*(102)->'pink'
printf("%s",**p+1)=ink.
(1)
Uma B B said:
6 years ago
3rd line//initially P is pointing to S+3={"Violet"}
4th line//++p ;p is pointing to address of string S+2={"pink"}
5th line//**p+1;**p is pointing to string "pink"
**P+1//p pointing to ink.
4th line//++p ;p is pointing to address of string S+2={"pink"}
5th line//**p+1;**p is pointing to string "pink"
**P+1//p pointing to ink.
(11)
RAHUL KUMAR said:
1 decade ago
In first line there are four character arrays
s+3 means s[3] means voilet
s+2.......etc
**p+1 will first point to s2 and in that string control will move one
String forwad and print that
i.e s[3]=pink
**p+1=ink
s+3 means s[3] means voilet
s+2.......etc
**p+1 will first point to s2 and in that string control will move one
String forwad and print that
i.e s[3]=pink
**p+1=ink
Sumathi said:
1 decade ago
main(){int x=2,p*,q*;
p=q=&x;
*q+=*p+=a+=4;
printf("%d%d%d",x,*p,*q);
}
The output of this program is x=24 *p=24 *q=24.
I can't understand the program. Will anyone please explain this program?
p=q=&x;
*q+=*p+=a+=4;
printf("%d%d%d",x,*p,*q);
}
The output of this program is x=24 *p=24 *q=24.
I can't understand the program. Will anyone please explain this program?
Emanuel said:
9 years ago
I think it doesn't work.
The 1st line keeps the array of char pointers. The array of addresses of the first team of each char array, and who will keep the each char array itself?
Correct me, if I am wrong.
The 1st line keeps the array of char pointers. The array of addresses of the first team of each char array, and who will keep the each char array itself?
Correct me, if I am wrong.
Lakshmi said:
1 decade ago
@suchismita.
We can print white as:
printf("%s",++s);
or
Give *s as a pointer to any varibale name, you can indirectly do so
printf("%s",s+1);
Here
printf("%s",*s+1);
We can print white as:
printf("%s",++s);
or
Give *s as a pointer to any varibale name, you can indirectly do so
printf("%s",s+1);
Here
printf("%s",*s+1);
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers