Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

References

Status
Not open for further replies.

Stopher

Programmer
Jan 12, 2002
29
US
Hi,
Does anyone know how to define references in C++ 6.0?

Thanx,
Stopher
 
I'm thinking about a feature that allows you to create an alias for a variable, and use either name to access the variable.

Thanx,
Stopher
 
did u mean something like this

int a=1,*b;
b=&a;
printf("a=%d *b=%d\n",a,*b);
*b=2;
printf("a=%d *b=%d\n",a,*b);

as pasing b value 2 will change a as well
 
To elaborate on what pankajkumar wrote, *variable creates a "pointer" to an address in memory.


int *ptr; //creates a pointer to an integer

------------------------------------------------------
The & operator returns an address in memory. Let's say int myInt has an address of 0001 (just pretend).


int myInt = 5;
&myInt; //would return 0001, not 5

--------------------------------------------------------
Since ptr "points" to an address,


ptr = &myInt; //assigns the address of myInt to the pointer
//Again, since ptr points to an address
cout << ptr << endl; //would print out 0001, not 5

---------------------------------------------------------
To &quot;dereference&quot; a variable (get its value instead of it's address) use the * operator like so:

cout << *ptr << endl; //prints 5 instead of 0001
 
Oh, and saying *ptr = 6 would change the value of myInt to 6.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top