# refereces versus pointers in functions

I went to the university and tried to get a hold of the professor ( who is actually a doctor of computer science who runs the who computer science department) but couldnt get a hold of him because he had left all ready.

But i did talk to a computer science teacher

she said that

references are "passed by reference" and that pointers are data types.

she explained to me how references work

for example

double void test(int& x)<-- decloration before int main

int main()

at this point the variable x has been declared and that means that variable x takes up a certain memory spot that is a double.........lets say that it takes up memory spot number 1101

after int main you declare other variables that will be used by the test function

like this

double w,x,y,a,b,c;

then you put down a function call,(the function call is all ways on the right hand side of the = sign)

bill = double void test (int& x)

so at this point the memory for variable is still at memory location 1101

the function call goes down to the function body and then returns back to the function call that gives out a total which is where the word bill is at.

so now the program goes on

now comes another function cal but instead of it being variable x, you change it to variable w, which was delcared right after the int main()

so now the function call looks like this

double void test(int& w)

the thing here is that w doesnt take the memory space of 1101, it takes the memory space of 1120

the teacher told me that the memory location at 1101 , which is variable x , is pointing at the memory location for variable w, which is at memory location 1120

its simular to pointers , except that its not

heres why.

as the program continues to go on another function call will come up

instead of it being x or w, it will be a.

double void test (int& a);

variable a will have its own memory place, lets just say that variable a will take memory spot 1130

now heres the thing

when the variable changes from x to w then that means that x ispointing to w and when the variable changes from w to bing a , that means that x will stop pointing to w and then point to a.

she also told me that what ever the varaible that comes up in the parameter ( w, a, ect) that it will always be pointed to by x and that what ever variable it was pointing will stop pointing to that variable. each variable ( w,a, ect) that is declared will have its own memory spot ( w=1120, a=1130)

so this means that i can change the name of the variables.......from x to a or be or what ever as many times as i want as long as i declare them. This means that this kind of process can be infinate.....not that it actually would be.

I asked her about incrementing and decrementing pointers and functions

she didnt say anything about incrementing and decrementing pointers but she did say that you can increment and decrement functions

heres how she explained it:

double void test (int& x)

this means that x is in memory space 1101.

lets say you put a value on varible x and say that its 2.00

so this would mean that 2.00 would be in memory space 1101 and that memory space 1101 is the variable int& x

she said you can create a formulat like this

x = x + 1

if this formula was applied to the program then that menas that the 2.00 would change to 3.00

so incrementing and decrementing of values in variables of refernce parameters can be done.

I dont know anything about pointers so i cant say anything about them and she didnt say anything about them. Pointers will come in time for me

so its

passed by reference and pointers are a data type.

thank you

• [code]
void foo(int& x)
{
x = 123;
}

int main()
{
int a = 0;
foo(a);
cout << "a = " << a << endl;

}
[/code]

[blue]The above code main() passes variable a by reference to function foo(). Inside function foo() the variable is named x. variable x is still a reference to a in main() even though it has a different name. You can name it anything you want and it will still be a reference to variable a in main(). Next, foo() sets the value of varialbe x to 123. When the programs returns to main(), variable a will now contain 123, as set in foo().

Compile and run the program to see how it works. Add additional code to see what happens.

[code]
void foo(int* x)
{
*x = 123;
}

int main()
{
int a = 0;
foo(&a);
cout << "a = " << a << endl;

}
[/code]

A pointer can be (but not necessary always is) the same as c++ references. See above code for difference syntax. There are only a few minor differences between pointers and references.

[/blue]

• [blue]A nice trick is when a function RETURNS a reference, like so:
[code]
int a = 7;

int& value ()
{
return a;
}
[/code]
Then assignment can work both ways!
[code]
// 1.
int another = value (); // 'another' will receive 7

// 2.
value () = 23; // 'a' from above will receive 23
[/code]
[/blue]
• : [blue]A nice trick is when a function RETURNS a reference, like so:
: [code]
: int a = 7;
:
: int& value ()
: {
: return a;
: }
: [/code]
: Then assignment can work both ways!
: [code]
: // 1.
: int another = value (); // 'another' will receive 7
:
: // 2.
: value () = 23; // 'a' from above will receive 23
: [/code]
: [/blue]
:

I find that very confusing, is it used ofthen? I saw it first a couple of days ago, before never...
• I cant see how you get 23 out of this:

: int a = 7;
:
: int& value ()
: {
: return a;
: }
:

the problem im having is that i cant see what int& value() is doing?
• : I cant see how you get 23 out of this:
:
: : int a = 7;
: :
: : int& value ()
: : {
: : return a;
: : }
: :
:
: the problem im having is that i cant see what int& value() is doing?
:
He's asigning 23 to a. Even if he can't access a. The reference returns the address of a, and then he asigns 23 to that address. This could be done similar with pointers I think...

Happy coding wishes
the one and only
[b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]