By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

what is the difference between int& and int

P: 2
hello,

In some code i see some definitions like that "int& a" and I wonder what is the difference with "int a".

By the way, is it different between "char* c" and "char *c "

Thank you very much and best regards,
starbucks
Jun 2 '07 #1
Share this Question
Share on Google+
8 Replies


AdrianH
Expert 100+
P: 1,251
hello,

In some code i see some definitions like that "int& a" and I wonder what is the difference with "int a".

By the way, is it different between "char* c" and "char *c "

Thank you very much and best regards,
starbucks
:smirk: Newbies. LOL, JK.

Seriously, your question is one that some people have difficulties with. C/C++ don't care about whitespaces and even less about whitespaces or lack thereof between punctuation and words/punctuation.
Expand|Select|Wrap|Line Numbers
  1. char* c
is equivalent to
Expand|Select|Wrap|Line Numbers
  1. char *c
is equivalent to
Expand|Select|Wrap|Line Numbers
  1. char*c
is equivalent to
Expand|Select|Wrap|Line Numbers
  1. char
  2.  
  3. *
  4.  
  5.  
  6. c
As for int& a, that is a declaration of a reference to an int. It is required to be initialised to something, unlike a int * which you don't have to initialise (though you probably should). Think of it like borrowing an int, or aliasing an int. Take this for example:
Expand|Select|Wrap|Line Numbers
  1. int a = 5;
  2. int & refA = a;
  3. refA = 6;
  4. printf("a == %d, refA == %d\n", a, refA);
  5. printf("%p == %p", &a, &refA);
  6.  
You can reference any type, be it a base type or user defined type.

Hope this helps. Oh, and the only stupid question is the unasked one. Don't feel bad, I'm just in a weird mood. ;)


Adrian
Jun 2 '07 #2

Savage
Expert 100+
P: 1,764
Oh, and the only stupid question is the unasked one. Don't feel bad, I'm just in a weird mood. ;)


Adrian
Adrian, u realy should get off from that crack.

:)

Savage
Jun 2 '07 #3

AdrianH
Expert 100+
P: 1,251
Adrian, u realy should get off from that crack.

:)

Savage
Then stop supplying it. :p


Adrian
Jun 2 '07 #4

P: 2
yes, I don't mind. Thank you for your help

-- starbucks
Jun 2 '07 #5

AdrianH
Expert 100+
P: 1,251
yes, I don't mind. Thank you for your help

-- starbucks
No prob. Come on back now, hear?


Adrian
Jun 2 '07 #6

Savage
Expert 100+
P: 1,764
Then stop supplying it. :p


Adrian
Adrian,u are a genius.

If we make Mary depand on our cracks,then she will need to cure herself and
rehabilitate,so she won't be able to post anymore.

Muhahahahahaha,muahhahaha

<cough>

hahahha

Savage
Jun 3 '07 #7

Expert 10K+
P: 11,448
Seriously, your question is one that some people have difficulties with. C/C++ don't care about whitespaces and even less about whitespaces or lack thereof between punctuation and words/punctuation.
Try a class template using a class template as its type parameter; you'll end up
with two '>' characters; you bet you have to put a space between them if you
don't want to upset your compiler ;-)

kind regards,

Jos
Jun 3 '07 #8

AdrianH
Expert 100+
P: 1,251
Try a class template using a class template as its type parameter; you'll end up
with two '>' characters; you bet you have to put a space between them if you
don't want to upset your compiler ;-)

kind regards,

Jos
That is an exception. In the new draft of the standard, this problem is to be eliminated.


Adrian
Jun 3 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.