473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

what is the difference between int& and int

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
8 19564
AdrianH
1,251 Expert 1GB
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
1,764 Expert 1GB
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
1,251 Expert 1GB
Adrian, u realy should get off from that crack.

:)

Savage
Then stop supplying it. :p


Adrian
Jun 2 '07 #4
yes, I don't mind. Thank you for your help

-- starbucks
Jun 2 '07 #5
AdrianH
1,251 Expert 1GB
yes, I don't mind. Thank you for your help

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


Adrian
Jun 2 '07 #6
Savage
1,764 Expert 1GB
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
JosAH
11,448 Expert 8TB
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
1,251 Expert 1GB
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

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

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
1
by: Bo Sun | last post by:
hi: please take a look at the following code: void* vp; int vp_test = 20; vp = & vp_test;
7
by: Shan | last post by:
Hi all, In the following code the line *(int *)&i = 11; is confusing to me. What is it doing ? #include<iostream> using namespace std; int main() { const int i = 10; *(int *)&i = 11;
2
by: Andy | last post by:
Hi, Could you explain why vectora<int&> is not allowed? For example, the following prgram will be given compilation error. #include <vector> using namespace std; int main() {
8
by: aling | last post by:
Given following code snippets: int a; int* p=(int*)((&a)+1); when I debuged the code using IDE like VC7.1, I found that: a = 0x0012fc50 (int ) p = 0x0012fc78 (int *) (&a)+1 = 0x0012fc51...
16
by: | last post by:
I saw this code in C++ but when tried to C causes an error: ------------- void function(int &a) { a = 5; } ------------- with this, passed in "function" the "a" pointer instead of "a" value, BUT...
5
by: c language | last post by:
Hello everybody, I have an array of numbers that I want to put them in a string. I have an example that you could complete it if you could or just let me know how I can do that. Thank you very...
3
by: Michael | last post by:
Hi, I am a little confused about & and int &. Do they both mean reference? int a; int* b= & a ; (& here mean reference of a) int& b = a ; ( How to understand & precisely here?) Thanks...
3
by: bingfeng | last post by:
hello, anyone else can explain why following codes give wrong result while compiler accept it still? int & foo() {} int main() { int x = foo; std::cout << x << std::endl; }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.