![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: pass by reference, pointer |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| Some Info on Pointers As requested... here's a little info on what pointers are ![]() Pointers are just what they say - it "points" do another location in memory. Its just like when you pass by reference vs. pass by value to a function. You are literally editing that object you passed in - not a copy of the object. Code: #include <iostream>
using namespace std;
int main() {
int a, b;
int * p;
a = 1;
b = 2;
cout << "A is equal to " << a << endl;
cout << "B is equal to " << b << endl;
// You can see here that a and be were set to values
p = a;
cout << "p was set to have the same value as a: " << p << endl;
p = &a;
cout << "p is now set to 'POINT' to 'a' so 'p' can control a`s value.";
p = 3;
cout << "a was changed because p was changed to: " << a << endl;
return 0;
} __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
| The Following 3 Users Say Thank You to ccoonen For This Useful Post: | ||
| |
| ||||
| Thanks for the information, i am going to test it out when i get back from my holiday in a week so i remember it. |
| |||
| I'm afraid the example doesn't compile on anything I tested it with, because the compiler sees you trying to copy an integer object to a pointer object in the first case, and an integer literal to a pointer object in the second case. Unlike C++ references, pointers do not merely alias another object - They are objects themselves, with their own data type, and they occupy a location in memory. They can mimic references by storing a data value which represents an address in memory (Hopefully an address of another valid object belonging to your program). But they are far more versatile than references, (hence why pointer syntax is more complicated than that of a reference too), and because you can do some pretty unsafe stuff with pointers, the compiler is trained to check when you're about to do something extremely bad. Here was the first line that MSVC 2003 choked on Code: p = a; Code: error C2440: '=' : cannot convert from 'int' to 'int *' The '=' operator (assignment operator) attempts to copy some int data (the integer 1) into a memory location designated for address data. The two types are generally incompatible, and the compiler rightly flags up an error. Suppose for a moment that the compiler allowed you to do this, your int data was integer 1 and you later attempted to access a memory location represented by 1 (0x00000001), you're almost certainly going to be trying to look into a memory location which doesn't belong to your program.. What happens next is anyones guess! but you're most likely to crash'n'burn. There are ways around doing this, you can use a reinterpret_cast to assign an integral value to a pointer object, but you're dicing with death if you do this - most OS's will just throw up a segfault or some other nastie if you actually try dereferencing a pointer when the memory address doesn't belong to your program - the standard, naturally, leaves its behaviour undefined. Code: p = reinterpret_cast<int*>(a); //Don't do this, unless you
// really know what you're doing! - Luckilly, it sticks out in your code like a sore thumb - so if you do use it, and you do start getting random crashes or weird behaviour, you can bet good money that reinterpret_cast is part of the code which is doing something horribly unexpected. (You can use a 'C' style cast too, which will do the same thing, but is even more "evil" because it hides itself away in your code rather nefariously) The next major error is this line Code: p = 3; Again, my compiler choked on the attempt to assign an integer data value of 3 to a memory location designated as an address value. But this time your intention seems to be to access the variable 'a' In order to access an object referred-by (or pointed-by) a pointer variable, you need to explicitly de-reference that pointer variable. In other words, your pointer is like a placeholder variable for the object (It remembers where the object is). You want to shove the pointer out the way, and gain direct access to the object.- You could think of a de-referencing operation like de-pointer-izing - the operation which grants access to the interested object. The de-reference operator is the asterisk symbol, same as that used in pointer declarations. In order to do what you intended, the line needs to change to look like this Code: *p = 3; Quote:
Here's a program which exhibits the behaviour you're describing Code: #include <iostream>
using namespace std;
int main()
{
int i = 5;
int& ref = i; //ref is an alias or nickname for variable i
cout << "Integer i is: " << i << '\n' ;
cout << "Variable aliased by ref is: " << ref << '\n';
ref = 10;
cout << "Integer i is: " << i << '\n' ;
cout << "Variable aliased by ref is: " << ref << '\n';
} Last edited by Bench : 08-16-2007 at 04:09 PM. |
| |||
| lol, yah - didn't have time to actually compile and test with compiler, thanks for further specifying. __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
![]() |
| Thread Tools | |
| Display Modes | |
| |