Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 10-09-2011, 12:06 AM   #1 (permalink)
Right Off the Assembly Line
 
bijay_ps's Avatar
 
Join Date: Mar 2011
Location: chennai
Posts: 44
Exclamation what is *&???


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??
__________________
Follow 7H3 G33K !nside YOU
bijay_ps is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 10-09-2011, 12:39 AM   #2 (permalink)
Electronic.
 
Neuron's Avatar
 
Join Date: Jun 2010
Location: Elsweyr
Posts: 530
Default Re: what is *&???

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.
__________________
I don't always make sense,but when I do,I don't.
Neuron is offline   Reply With Quote
Old 10-09-2011, 12:44 AM   #3 (permalink)
BIOS Terminator
 
nims11's Avatar
 
Join Date: Apr 2008
Location: Ranchi
Posts: 816
Default Re: what is *&???

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.
nims11 is online now   Reply With Quote
Old 10-09-2011, 08:13 AM   #4 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: what is *&???

GATE exam has void main() in its question? Or is it just with the study material?

Quote:
Originally Posted by Neuron View Post
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 - http://stackoverflow.com/questions/5...tring-literals
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline   Reply With Quote
Old 10-09-2011, 10:27 AM   #5 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,675
Default Re: what is *&???

So the answer is H ?

Amidoinrite ?
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is online now   Reply With Quote
Old 10-09-2011, 01:05 PM   #6 (permalink)
Electronic.
 
Neuron's Avatar
 
Join Date: Jun 2010
Location: Elsweyr
Posts: 530
Default Re: what is *&???

^^No
Spoiler:
Just kidding!


Quote:
Originally Posted by Liverpool_fan View Post

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.
__________________
I don't always make sense,but when I do,I don't.
Neuron is offline   Reply With Quote
Old 10-09-2011, 01:06 PM   #7 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: what is *&???

The answer is Warning: main should always return int.,

P.S.: postlikethisoutsideprogrammingoss
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline   Reply With Quote
Old 10-09-2011, 09:41 PM   #8 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Re: what is *&???

^^^
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!!
abhijangda is online now   Reply With Quote
Old 10-09-2011, 09:48 PM   #9 (permalink)
Sami Hyypiä, LFC legend
 
Liverpool_fan's Avatar
 
Join Date: Jun 2007
Location: Нью-Дели
Posts: 2,138
Default Re: what is *&???

^ 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
__________________
Experience true education in Computer Science - http://www.udacity.com | http://www.coursera.org

Spoiler:
Read before asking / messaging any moderator for any query: FAQ + answers for new members

Read all the sticky threads before asking any type of query. Most basic questions are answered in those.
Don't use forum for chatting. Visit http://webchat.freenode.net/?channels=krow, enter nick and connect.
Liverpool_fan is offline   Reply With Quote
Old 12-09-2011, 12:16 AM   #10 (permalink)
Right Off the Assembly Line
 
bijay_ps's Avatar
 
Join Date: Mar 2011
Location: chennai
Posts: 44
Default Re: what is *&???

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

Quote:
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
__________________
Follow 7H3 G33K !nside YOU
bijay_ps is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:34 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2