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 2 of 4.

Deepika said:   9 years ago
Very well explained @Deepu.

User said:   9 years ago
Because * is called dereference and & is called reference so * is cancel with &.

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)

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.

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)

Jogamohan Medak said:   1 decade ago
Ans:[B] Because:

Given p="hello"

p=*(&p)=*&p

p=*&p=*(&(*&p))

p=*&*&p=hello

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)

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.

Vishal said:   1 decade ago
Thanks shamini

Sarika said:   1 decade ago
Thanks samini.


Post your comments here:

Your comments will be displayed after verification.