473,570 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what is the difference between int& and int

2 New Member
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 19578
AdrianH
1,251 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
Adrian, u realy should get off from that crack.

:)

Savage
Then stop supplying it. :p


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

-- starbucks
Jun 2 '07 #5
AdrianH
1,251 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert MVP
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 Recognized Expert Top Contributor
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
16228
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 integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? ...
1
1317
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
1566
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
1700
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
4707
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 (char *) (&a)+2 = 0x0012fc52 (char *)
16
7014
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 inside "function" I have access to "a" value not with "*a" but with "a". Why this is not working in C?
5
2235
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 much, MJ #include <stdio.h> #define I 10
3
2339
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 in advance,
3
2573
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
7948
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7700
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6329
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5523
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.