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 3 of 11.
Mamatha said:
1 decade ago
*s=s[0],s[1],s[2],s[3] {"black","white","pink","violet"}
s[0]=black, s[1]=white,s[2]=pink,s[3]=violet
**ptr=s+3 means we can write s[3] in the same way
s+2=s[2], s+1=s[1],s=s[0]
++p it can points the adress of the **ptr in this **ptr the first adress of variable is s+3
++p=s+3
after p=s+2
*p=s+2
**p=s[2]=pink
**p+1=ink
s[0]=black, s[1]=white,s[2]=pink,s[3]=violet
**ptr=s+3 means we can write s[3] in the same way
s+2=s[2], s+1=s[1],s=s[0]
++p it can points the adress of the **ptr in this **ptr the first adress of variable is s+3
++p=s+3
after p=s+2
*p=s+2
**p=s[2]=pink
**p+1=ink
Pranav said:
1 decade ago
ptr = {pointer to "violet", pointer to "pink", pointer to "white", pointer to "black"}
p = ptr --> *p = pointer to "violet"
++p --> *p = pointer to "pink"
This implies that:
*p = {'p','i','n','k','\0'}
Which means:
**p = 'p'
**p + 1 = 'i'
so **p + 1 is a pointer to this string: {'i', 'n', 'k', '\0'}, which is simply "ink"
p = ptr --> *p = pointer to "violet"
++p --> *p = pointer to "pink"
This implies that:
*p = {'p','i','n','k','\0'}
Which means:
**p = 'p'
**p + 1 = 'i'
so **p + 1 is a pointer to this string: {'i', 'n', 'k', '\0'}, which is simply "ink"
Sandeep kumar said:
1 decade ago
ptr[0] points to char[]={"v","o","i","l","e","t"}
ptr[1] points to char[]={"p","i","n","k"}
ptr[2] points to char[]={"w","h","i","t","e"}
ptr[3] points to char[]={"b","l","a","c","k"}
now p is pointing to ptr //p = ptr
and ++p now lets pointer p to point to ptr[1] i.e.
char[]={"p","i","n","k"} as it is a pointer to an array
now most imp thing **p+1 means **(p+1) i.e it points to "ink"...
ptr[1] points to char[]={"p","i","n","k"}
ptr[2] points to char[]={"w","h","i","t","e"}
ptr[3] points to char[]={"b","l","a","c","k"}
now p is pointing to ptr //p = ptr
and ++p now lets pointer p to point to ptr[1] i.e.
char[]={"p","i","n","k"} as it is a pointer to an array
now most imp thing **p+1 means **(p+1) i.e it points to "ink"...
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
Thirum said:
1 decade ago
*s[] = {"black", "white", "pink", "violet"};
ptr = {"violet","pink","white","black"};
p = ptr;// means "violet"
++p ;//means "pink"
printf("%s", **p+1);//**p+1 point to "i" of pink so it will print "ink"
So ink is output
Which is simply "ink".
ptr = {"violet","pink","white","black"};
p = ptr;// means "violet"
++p ;//means "pink"
printf("%s", **p+1);//**p+1 point to "i" of pink so it will print "ink"
So ink is output
Which is simply "ink".
Rana said:
1 decade ago
Plese help me in how pointer point p with example showing ** means and ++p.
Aakash said:
1 decade ago
When we use ++p ie we r pre incrementing the address which is stored at p if p is an double pointer ie pointer to pointer
for example:
A knows address of B, and C Knows address of A then in order to
find B, C has to refer to A then thro' A he'll find B
so here A is an single pointer
and C is an double pointer..
for example:
A knows address of B, and C Knows address of A then in order to
find B, C has to refer to A then thro' A he'll find B
so here A is an single pointer
and C is an double pointer..
Hai said:
1 decade ago
Plese help me in how pointer point p with example showing ** means and ++p.
Veeraselvi said:
1 decade ago
What is the difference between *p and **p? Example c & Java program.
Any one please tel me the answer?
Any one please tel me the answer?
Suchismita said:
1 decade ago
How can we print WHITE by using s pointer?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers