what is *&???

bijay_ps

Broken In
I got this question from a study material for GATE exam. The code is as follows:
Code:
#include<stdio.h>
void main()
{

 char *p;

 p="Hello";

 printf("%c\n",*&*p);

}
someone please explain me what is this *&*p and what and how it does??
 

Neuron

Electronic.
It's actually a tricky question from 'pointers' in c.
char *p is a character type pointer.It means the the content at the address p, is of type char.
& is, well you can say, is an inverse operator of *.means &* just cancel each other of.
So 'printf("%c\n",*&*p)' is just 'printf("%c\n",*p)'
Since *p is a char and not a group of characters, p="Hello" will only store 'H' at the address p.
p=>an address *p=>content at that address.

This is basic stuff .You should have figured it out on your own.
 

nims11

BIOS Terminator
for convenience,
consider *&*p as *(&(*p))

p has the address to 'H'. it is first dereferenced using *, then again referenced using & and then again dereferenced using * to 'H'.

in your case, &(*p)=p.
 

Liverpool_fan

Sami Hyypiä, LFC legend
GATE exam has void main() in its question? Or is it just with the study material? :|

Since *p is a char and not a group of characters, p="Hello" will only store 'H' at the address p.
Actually "Hello" is a string literal, and p = "Hello" will point the address of p to the "read only" memory location where "Hello" is stored.
Clarification - *stackoverflow.com/questions/5342511/c-null-pointer-with-string-literals
 

Neuron

Electronic.
^^No
Just kidding!:mrgreen:

Actually "Hello" is a string literal, and p = "Hello" will point the address of p to the "read only" memory location where "Hello" is stored.
Clarification - c++ - C null pointer with string literals - Stack Overflow

True.I get it. printf("%s",p) will print the entire string literal.
 

abhijangda

Padawan
^^^
i think you are using g++.
Just change void to int and add return 0 at the end.
Actually it is always good to return integer through main, to know if program is exited properly!!
 

Liverpool_fan

Sami Hyypiä, LFC legend
^ er... I didn't even bother to compile it.
Anyway, it's not only it's good to use int with main, using anything else apart from int with main violates the ANSI standards, and that's the end of that.
Clarification - void main(void) - the Wrong Thing
 
OP
bijay_ps

bijay_ps

Broken In
actually in the question it was just main() only. And the answer is H. And thnx for all your replies I understood the concept

This is basic stuff .You should have figured it out on your own.
Yes Neuron I should have tried it by myself but I was a bit impatience.... and thnx for your explanation buddy
 
Top Bottom