C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 17)
17.
What will be the output of the program ?
#include<stdio.h>
int main()
{
char *p;
p="hello";
printf("%s\n", *&*&p);
return 0;
}
Discussion:
37 comments Page 1 of 4.
Deepu said:
1 decade ago
Guys one important of pointers is
* stands for value at address
& address of
Since p is a pointer variable which stores only the address it contains address of h (i.e base address of let us say 500)
printf("%s\n",*&*&p);
*&*&p - value at address( address ( value at address ( address ( p))))
-> value at address( address ( value at address ( 500)))
-> value at address( address ( h))
-> value at address(500)
-> h
Since %s represents a string it prints starting from h which is hello.
hope this helps!
* stands for value at address
& address of
Since p is a pointer variable which stores only the address it contains address of h (i.e base address of let us say 500)
printf("%s\n",*&*&p);
*&*&p - value at address( address ( value at address ( address ( p))))
-> value at address( address ( value at address ( 500)))
-> value at address( address ( h))
-> value at address(500)
-> h
Since %s represents a string it prints starting from h which is hello.
hope this helps!
(21)
Srinika said:
7 years ago
Hello, can anyone explain me this?
Char *p.
Here *p is a pointer so it can store the only address of a character. But we are assigning it a string? How is that possible?
Char *p.
Here *p is a pointer so it can store the only address of a character. But we are assigning it a string? How is that possible?
(1)
Abhay said:
1 decade ago
@Deepu is right and people who saying p="hello" are wrong because p is a pointer so *p=h; since it is string it will print hello.
(1)
Anuradha said:
9 years ago
Referencing and address operator are canceling each other.
Here is the direct assignment to the pointer. So hello is printed.
If char *p="hello".
P have an address of h only.
Here is the direct assignment to the pointer. So hello is printed.
If char *p="hello".
P have an address of h only.
(1)
Wasi said:
9 years ago
Whenever * and & comes we just need to simply cancel.
for e.g
p=10;
q=&p;
t=*&p--->means t=p
And if we print *&p it will print 10.
for e.g
p=10;
q=&p;
t=*&p--->means t=p
And if we print *&p it will print 10.
(1)
Abhijeet said:
9 years ago
Very helpful, Thanks everyone.
Rishabh said:
1 decade ago
Thumb Rule for pointers:
'*' and '&' are like 'x' and '1/x', so will cancel each other whenever place together.
Hope this makes it simple to understand.
'*' and '&' are like 'x' and '1/x', so will cancel each other whenever place together.
Hope this makes it simple to understand.
Jogamohan Medak said:
1 decade ago
Ans:[B] Because:
Given p="hello"
p=*(&p)=*&p
p=*&p=*(&(*&p))
p=*&*&p=hello
Given p="hello"
p=*(&p)=*&p
p=*&p=*(&(*&p))
p=*&*&p=hello
Ashraf said:
1 decade ago
Best answer @Deepu.
I believe your answer is the only correct logic, because if we go by earlier mentioned logic's then it should print p where p stores the address of "Hello".
Thanks.
I believe your answer is the only correct logic, because if we go by earlier mentioned logic's then it should print p where p stores the address of "Hello".
Thanks.
User said:
9 years ago
Because * is called dereference and & is called reference so * is cancel with &.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers