Jump to content
ChetanaSforum
Sign in to follow this  
chitkaraankit

c++ predict output

Recommended Posts


main() { char *p; p="Hello"; printf("%c\n",*&*p); }

I think the question is:main() { char *p; *p="Hello"; //'*' was not in the given Question printf("%c\n",*&*p); }and then output must be : Hello

Share this post


Link to post
Share on other sites

Ans for this question is CHAR is a 1 BYTE'H'bcos you declared the pointer variable as CHAR and in printf statement there is no meaning for *&, so it ll print only 'H'

Share this post


Link to post
Share on other sites

Ans for this question is

 

CHAR is a 1 BYTE

 

'H'

bcos you declared the pointer variable as CHAR and in printf statement there is no meaning for *&, so it ll print only 'H'

A block of memory is reserved for "Hello" and the pointer variable "p" is set to point to the first character, which in this case is "H".

 

In memory, "Hello" will be represented as follows. For instance, if the starting address is 1000, the arrangement would be :

 

1000 : H

1001 : e

1002 : l

1003 : l

1004 : o

1005 : '\0'

 

*p = *(p+0) <-- You can use either of these notations to access the element of an array.

 

So, considering the printf statement - *&*p

 

*p will now contain 'H'

 

&(*p) will contain the address corresponding to 'H' - in this case - 1000 (varies with different compilers)

 

Again, *(&*p) will return the value at address 1000, which in this case is 'H'.Posted Image

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  



×