Help me Understand the use of '&'

Page 1 of 1

13 Replies - 6945 Views - Last Post: 01 June 2011 - 08:49 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=234152&amp;s=fc014eae0c91a7b48903232d4b5ac361&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Help me Understand the use of '&'

Posted 01 June 2011 - 12:06 AM

Beginning to learn C++, I wrote this simple code for Fibonacci Sequence.

```#include <conio.h>
#include <iostream>
using namespace std;
void newLine();
//The function prototype does not need to know the exact variables, it only needs to know the data types
void Results(int, int &, int &, int &, int &, int &, int &, int &);
void main ()
{
char name[35] = "";
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int f=0;
int g=0;
int h=0;
cout << "Enter your name: ";
cin.getline(name, 35);
newLine();
cout << "Hello " << name;
newLine();
newLine();
cout << "Enter a number: ";
cin >> a;
newLine();
cout << "Your Fibonacci Series is: ";
newLine();
newLine();
Results(a, b, c, d, e, f, g, h);
_getch();
}

void newLine()
{
cout << "\n";
}
//Here one needs to put the value of the exact variables

void Results(int a, int &b, int &c, int &d, int &e, int &f, int &g, int &h)
{
h=a-1;
b=a+(a-1);
c=a+b;
d=b+c;
e=c+d;
f=d+e;
g=e+f;
cout << " " << b;
cout << " " << c;
cout << " " << d;
cout << " " << e;
cout << " " << f;
cout << " " << g;
_getch();
}
```

In this tutorial, the tutor says about the use of '&' and about memory address passing etc, but I really could not get the point. My simple code works exactly the same with or without the '&'. Please help me understand the use of '&'.
P.S. I am just a beginner

Is This A Good Question/Topic? 0

Replies To: Help me Understand the use of '&'

#2 Salem_c

• void main'ers are DOOMED

Reputation: 2159
• Posts: 4,222
• Joined: 30-May 10

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 01:23 AM

The point would be to put lines 50 to 55 in main.

FWIW, following tutorials written for an old compiler (VC6), using non-standard header files (iostream.h) and a "void main programmer", you need to have a large bucket of salt to hand.

#3 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 01:29 AM

Salem_c, on 01 June 2011 - 01:23 AM, said:

The point would be to put lines 50 to 55 in main.

I know it could be simply put under the main() function. However the point of the tutor was to teach functions, so in that respect the code is perfectly OK. I just wanted an in-depth reference to the use of '&', not to mention, the tutor actually teaches very well

#4 Salem_c

• void main'ers are DOOMED

Reputation: 2159
• Posts: 4,222
• Joined: 30-May 10

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 01:41 AM

There is no point to using & in that context then.

There is also no point in passing a load of parameters to the function either, if all you're going to do is print them out inside the function.

The whole point of reference parameters is to get a result OUT from the function, so you can use it elsewhere.

> not to mention, the tutor actually teaches very well
Do not mistake an easy style for competence and technical accuracy.

There are a lot of programming books which I would put in the "fiction" section. An entertaining read no doubt, but by and large useless for learning a technical discipline.

#5 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 02:22 AM

Salem_c, on 01 June 2011 - 01:41 AM, said:

There is no point to using & in that context then.

There is also no point in passing a load of parameters to the function either, if all you're going to do is print them out inside the function.

The whole point of reference parameters is to get a result OUT from the function, so you can use it elsewhere.

> not to mention, the tutor actually teaches very well
Do not mistake an easy style for competence and technical accuracy.

There are a lot of programming books which I would put in the "fiction" section. An entertaining read no doubt, but by and large useless for learning a technical discipline.

Let's forget about the teacher now. I just wanted to know the use of "&" in the C++ language. Let's not convert this post into a critical review

This post has been edited by Jeet.in: 01 June 2011 - 02:23 AM

#6 Bench

• D.I.C Lover

Reputation: 944
• Posts: 2,464
• Joined: 20-August 07

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 02:29 AM

Jeet.in, on 01 June 2011 - 09:29 AM, said:

Salem_c, on 01 June 2011 - 01:23 AM, said:

The point would be to put lines 50 to 55 in main.

I know it could be simply put under the main() function. However the point of the tutor was to teach functions, so in that respect the code is perfectly OK. I just wanted an in-depth reference to the use of '&', not to mention, the tutor actually teaches very well

The first thing you could do is try Salem_c's suggestion, and then you'll see what happens when you omit the &. Otherwise perhaps your video tutorials will eventually explain reference-vs-value parameters.

Buying a good book would probably be the best answer in terms of having good quality in-depth information at hand; if for whatever reason you can't/don't want to spend the money, then there are a lot of basic language references online. I strongly recommend using Google to answer basic language questions like this (no doubt you'll end up with many more along the way) - you'll need to do a bit of research and follow a few different links, but a lot of the information is already out there.

Here are a few links which might help:
http://publib.boulde...110.htm#cplr110
http://www.parashift...references.html
http://www.cprogramm...references.html
http://en.wikipedia....e_%28C%2B%2B%29

This post has been edited by Bench: 01 June 2011 - 02:29 AM

#7 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 02:32 AM

Thank you so much. I will do try to study and gain as much information possible. Between, any good books you recommend ?

This post has been edited by JackOfAllTrades: 01 June 2011 - 08:41 AM
Reason for edit:: Removed unnecessary quote

#8 Bench

• D.I.C Lover

Reputation: 944
• Posts: 2,464
• Joined: 20-August 07

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 02:48 AM

Jeet.in, on 01 June 2011 - 10:32 AM, said:

Thank you so much. I will do try to study and gain as much information possible. Between, any good books you recommend ?

The books which are usually considered "best of breed" for beginners are usually one of the following

• Accelerated C++ - Andrew Koenig / Barbara E. Moo
• C++ Primer 4th Ed. - Stan Lippmann, Josie Lajoie, Barbara E. Moo
• Programming - Principles and Practice using C++ - Bjarne Stroustrup

Accelerated C++ is the smallest of the 3 books (about 350 pages), but very correct, concise, and densely packed with information. (Very good if you're familiar with another programming language, maybe a bit heavy for complete novices but still well written)

C++ Primer is more of a "tome", and far more novice-orientated, with much longer explanations and examples. (And not to be confused with 'C++ Primer Plus' which is an inferior book by a different author)

Principles and Practice is written by the man who created the C++ language - he originally aimed it at his 1st-year university students; it's got a slightly broader scope than simply focusing on language basics; i.e. it tries to teach you how to be a programmer using C++ rather than simply explaining how the language works.

Also, Bruce Eckel's free e-book 'Thinking in C++' gets a lot of positive comments:
http://www.mindview....ingInCPP2e.html

Avoid any of the following books:
- C++ in X hours/days/etc
- C++ books for dummies/idiots/etc
- Anything ever written by Herbert Schildt

This post has been edited by Bench: 01 June 2011 - 02:54 AM

#9 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 08:17 AM

To the main question at hand: What is this &-operator all about?

In C/C++ the symbol & is "overloaded" meaning that it has different meanings in different contexts. Its simpelest meaning is:

number & number -- which forms an expression which results in the boolean AND of the two numbers. This really has little to do with your posts above but lets get it out of the way.

The next context is as the "address of" operator.

int* ptr = &someInt;

The address-of operator is used to form an expression that returns a pointer (i.e. an address or "reference to"). This is generally useful when you wish to make a pointer to something. For example when you want the results of some function to be stored at a location you have access to outside of a function:

scanf("%d", &result); -- here I am passing scanf (used in C for input) the address-of the variable "result" so that scanf can read a number from stdin and store it into that variable "result" - to do that, scanf needs to know where "result" is in memory - i.e. it needs the address, so we use the address-of operator.

In the context of programming, using an address to a variable is known as "referencing" and a "reference" is a fancy name for "an address" which more or less means "a pointer" (though a pointer is a variable that holds an address) -- but as we all know "Pointers are hard". In C++ there is a "polite" way to deal with pointers called "a reference" which lets you have all the power of pointers without all the hassle and * and -> operators.

I used to think of references as "aliases" because it lets you "rename" a variable:

```    int a = 10;
int& b = a;

cout << b << endl;

b++;

cout << a << endl;

```

Here we create an alias for a and call it b -- we can use b just as we can any other integer, but everything you do to b happens to a and visa-versa. "b" is a "reference" to "a". It is not exactly a pointer because unlike a pointer they may not be any particular place where the compiler stores the address of a. A pointer is a variable, and so its value is stored somewhere, but a reference does not need to take up any more memory that the variable it references.

by that I mean that when you declare a variable:

int someInt;

the compiler makes a "symbol" for the location of someInt and every time you use someInt the compiler uses that symbol to represent someInt directly in the resulting code (the symbol may represent an address if someInt is a global or static variable, or it may represent a stack offset for local variables). Note that it is the linker that actually replaces the symbols with their addresses. When you create a pointer a new symbol is created for the new variable and the address of the old variable is placed at the new location. -- When you create a reference, the compiler can just use the old symbol wherever it sees the reference -- i.e. there is not necessarily any reason to physically store the address.

So a reference is a kind of alias for a variable, you can use it just as you would the original, no need for the de-reference operator* or the referenced member operator-> nope you can skip all the little pointer semantics. IF you ask for the the "address-of" a pointer you get the address-of the pointer, if you ask for the address-of a reference you get the address of the original variable.

So this is nice. Now when I want to "pass by reference" I don't have to "pass by pointer" and I don't have to use all of those awful pointer semantics and just use that variable as I would a regular variable.
```void swap(int& a, int& b ) {
int temp = a;
a = b;
b = temp;
}

...

int var1 = 10;
int var2 = 20;

cout << "var1 = " << var1 << endl;
cout << "var2 = " << var2 << endl;

swap(var1, var2);

cout << "var1 = " << var1 << endl;
cout << "var2 = " << var2 << endl;

```

as opposed to the pointer version:

```void swap(int* a, int* b ) {
int temp = *a;
*a = *b;
*b = temp;
}

...

int var1 = 10;
int var2 = 20;

cout << "var1 = " << var1 << endl;
cout << "var2 = " << var2 << endl;

swap(&var1, &var2);

cout << "var1 = " << var1 << endl;
cout << "var2 = " << var2 << endl;

```

With the pointer version I have to use the address-of operator to pass into the function, then I have to use the de-reference operators to access the data... lots of symbols just to swap to values.

This post has been edited by NickDMax: 01 June 2011 - 08:51 AM
Reason for edit:: changed "logical AND" to "boolean AND"

#10 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 08:23 AM

Thank You Sooo..... much for giving such a detailed and to the point answer, something that I was truely looking for. Thaks a lot

Just thought would ask you, any book you would recommend for a beginner like me, that would help me learn C++?

Thanks Again

This post has been edited by JackOfAllTrades: 01 June 2011 - 08:42 AM
Reason for edit:: Removed unnecessary quote

#11 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 08:28 AM

NickDMax, on 01 June 2011 - 08:17 AM, said:

number & number -- which forms an expression which results in the logical AND of the two numbers. This really has little to do with your posts above but lets get it out of the way.

That's of course binary AND, not logical AND - logical AND is in the form of && operator. I'm sure that's just a typo.

Otherwise that's a very nice post.

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 08:45 AM

There is no need to quote the post directly above yours! It only serves to clutter the topic.

You've already been given book suggestions by Bench, there are FAQs for you to read, and here's a topic on books to read.

#13 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 308
• Joined: 30-May 11

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 08:48 AM

JackOfAllTrades, on 01 June 2011 - 08:45 AM, said:

There is no need to quote the post directly above yours! It only serves to clutter the topic.

Sorry Won't do that again

This post has been edited by Jeet.in: 01 June 2011 - 08:52 AM

#14 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Re: Help me Understand the use of '&'

Posted 01 June 2011 - 08:49 AM

Quote

That's of course binary AND, not logical AND - logical AND is in the form of && operator. I'm sure that's just a typo.

You are correct! Not so much as typo as a momentary lapse in language skills.

Quote

Just thought would ask you, any book you would recommend for a beginner like me, that would help me learn C++?

See Bench's post above. It has been a long time since I was a beginner. I really loved (and still look to it for guidance from time to time) a book called "Mastering Borland C++" by Tom Swan. However that book is VERY VERY OLD and does not approach the language from its modern perspective. The language has changed a great deal since then while much of the information would be non-standard and violate modern best-practices, some of it would be wrong in light of modern compilers.

I think that Bjarne Stroustrup's book "Programming - Principles and Practice" looks pretty good, although it is amusing to me that he extends his own language for the sake of the book. (note I have not read the book only skimmed though it in the bookstore).

I found "Thinking in C++" (which is a free e-book) to be pretty good (not sure what the current edition is like though).