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;
}
llo
hello
ello
h
Answer: Option
Explanation:
No answer description is available. Let's discuss.
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!
(21)

Amit said:   8 years ago
#include<stdio.h>

int main()
{
char *p;
p="hello";
printf("%s\n",*p);//it is not working why?
//*p means value at address

//pointer variable store the address of another variable but here hello is what.
return 0;
}

Shamini said:   2 decades ago
Easy way to remember * and &..

When char *p="hello";

scanf("%s",*&p);

It will print hello only.. becoz * and & will get cancel and so in this

problem *& and *& will get cancel and print hello only.

Rajesh said:   1 decade ago
see
p=&(*p);
*p=*&(*p);
so it will print *p;
actually *p is value and &(*p) is adress of value of p i.e nothing but p itself (base adress).
so now *(&*p) is nothing but he is asking about the value of p
thats it

Mohd kamarshad said:   1 decade ago
Because & is reference operator and * is a dereference operator
so *(&p) converted into value at the address
this processes again...
and eventually we would get output as
--> 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.

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.
(1)

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?
(1)

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.

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.
(1)


Post your comments here:

Your comments will be displayed after verification.