Pointers...

Started by
5 comments, last by Kaezin 22 years, 4 months ago
Does anyone know any links to a good site that can explain pointers? Or if anyone have any free time can they explain pointers? I usually understand everything I see in c++ EXCEPT pointers... thanks
Half the people you know are below average.Trogdor the Burninator
Advertisement
A pointer is a variable, which hold a pointer to a place in memory.

char some_char; // not a pointer
char *some_char_pointer; // a pointer

some_char_pointer = &some_char;
// some_char_pointer now point to the memory location of some_char

I have no time to explain now, but I will be back ...
Pavel "Corpse" Zuna
A pointer is a way of accessing a variable.
For instance:

int a = 5;
cout << a;

This creates a variable named a, and outputs it's value to the screen. Note that you have actually CREATED an int here.

int * b; //A POINTER to an int
b = &a //& means (memory address of)

This creates a POINTER to an int, and sets it to point to a. Note that you have NOT created an int here: just a pointer to one.

You can now do this:

cout << *b;

This will output the value of the variable pointed to by b (in other words a). The * is necessary when using pointers as otherwise it simply outputs the address of b.

You can also change the value of a through b.

*b = 8;

This sets the value of a to 8. (because b points to a).

I dunno if this is helping, but I'll just plod on anyway...

What you must understand is that when you create a pointer to an int (int * b), you are NOT creating an int. (It's the same with any other data type too) You are simply creating a name by which you can refer to an int. A pointer MUST point to something before it can be used.

int * b;
*b = 5;

This will not work, because b does not point to anything. You may not get an error, but it doesn't work

Now, as for why you might use pointers...

      void double(int x){ //Doubles the value of x x *= 2;}void main(){ int x = 5; double(x); cout << x << endl;}  


You might think that when the value of x is cout'ed, it would be ten, but it' not. It's still 5. This is because when you called double(x), it created a duplicate of x and passed it to double. Any changes made to x inside double were performed on the duplicate. So when you come back to main, x is still 5. We can fix this by using pointers, though. Instead of passing a duplicate of x into double, we are going to pass the MEMORY ADDRESS of x (a pointer). This will allow double to directly change the x in main.

  void double(int * x) //Now passes an int POINTER instead of value{ //Doubles the value of x *x *= 2;}void main(){ int x = 5; int * y = &x //Creates a pointer to x double(y); cout << x << endl;}  


You should find that x is now 10.

I know that pointers can be confusing, and also that someone trying to explain pointers can be doubly confusing, but I hope this has helped :D

P.S. This took some time to type, and I apologize if someone else has replyed in the meantime.

---------------

I finally got it all together...
...and then forgot where I put it.

Edited by - AdmiralBinary on November 19, 2001 5:02:51 PM

Edited by - AdmiralBinary on November 19, 2001 5:04:03 PM
Jeez that was fast... I''m at school ATM, left to go to the bathroom, and when I came back, I already had a couple replies Thanks for the huge explanation too. But I have a question: wouldn''t it be easier (I''m a newbie still) to just have the variable global instead? That way you don''t have to worry about parameters and the like... And also what about pointers to classes? Whats with the -> thing?
Half the people you know are below average.Trogdor the Burninator
Im new too, and I somewhat understand Pointers but do not understand why and when they should be used. In that example AdmiralBinary posted why not just do this?

void double(int &x)
{
//Doubles the value of x
x *= 2;
}

void main()
{
int x = 5;
double(x);
cout << x << endl;
}
MSN Messenger: Thomas_Szafran@hotmail.com
Tszafran, that basically uses a pointer in the background, it just hides it from you . References must always point to a valid variable, they can''t be NULL (a good and bad thing). They also cannot point to a dynamically allocated place in memory. Arrays are also treated as pointers.


[Resist Windows XP''s Invasive Production Activation Technology!]
Interesting, thanks for the reply. I was just getting into the dynamic memory stuff and they will probibly save me a headache.
MSN Messenger: Thomas_Szafran@hotmail.com

This topic is closed to new replies.

Advertisement