Re: [Dev-C++] problem with using reference
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2004-04-22 08:53:08
|
First printout you add 1 to a copy.
10+1 = 11. OK.
Second time you add 2 to the ORIGINAL 10+2 -> 12. OK:
Third time you add 3 to the ORIGINAL 10+2+3 = 15. OK:
Note that both pointer and reference access will update the original
variable!
/Per W
On Wed, 21 Apr 2004, faramarz yari wrote:
> hi
> i encounter a mysterous problem testing pointers and references in c++.
> i wrote this file :
> //*******************************************************************************
> #include <iostream>
> #include <cstdlib>
>
> using namespace std;
>
> void fv(int);
> void fp(int*);
> void fr(int&);
>
> int main (int argc, char *argv[])
> {
> int x=10;
> fv(x);
> system("PAUSE");
> fp(&x);
> system("PAUSE");
> fr(x);
> system("PAUSE");
> return 0;
> }
>
> void fv(int y)
> {
> y+=1;
> cout<<endl<<"*********************************"<<endl;
> cout<<endl<<"sending value to function"<<endl;
> cout<<endl<<"y : "<<y;
> cout<<endl<<"&y : "<<&y;
> cout<<endl<<"*********************************"<<endl;
> }
> void fp(int* ipx)
> {
> *ipx+=2;
> cout<<endl<<"*********************************"<<endl;
> cout<<endl<<"sending pointer to function"<<endl;
> cout<<endl<<"ipx : "<<ipx;
> cout<<endl<<"*ipx : "<<*ipx;
> cout<<endl<<"*********************************"<<endl;
> }
> void fr(int& irx)
> {
> irx+=3; //irx=irx+3;
> cout<<endl<<"*********************************"<<endl;
> cout<<endl<<"sending reference to function"<<endl;
> cout<<endl<<"irx : "<<irx;
> cout<<endl<<"&irx : "<<&irx;
> cout<<endl<<"*********************************"<<endl;
> }
> //************************************************************************************
>
> i expect these result :
> //*********************************************************
> y : 11
> &y : 0x241ff38 //for example
>
> ipx : 0x241ff5c //sth diferent from &y & it's ok
> *ipx : 12
>
> irx : 13 //but result in dev-c++ is irx : 15 ERROR
> &irx : 0x241ff5c //equal to ipx and &x and it's ok
>
>
> //*********************************************************
>
> i add irx with 3 and it should be 13 now,
> but as u can see it's 15
> i'm very eager to hear from u and knowing the answer that wath the problem
> is
> thank u in advance
>
> Faramarz Yari
>
> _________________________________________________________________
> MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
> http://join.msn.com/?page=features/virus
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: IBM Linux Tutorials
> Free Linux tutorial presented by Daniel Robbins, President and CEO of
> GenToo technologies. Learn everything from fundamentals to system
> administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|