Very basic question about pointers

Started by
10 comments, last by thedustbustr 16 years, 4 months ago
Is the only purpose of a pointer to manipulate data without dirrectly touching the variable being pointed to?
Advertisement
Well, no, since that doesn't really make any sense. A pointer to a variable is just an address to a variable. When modifying through a pointer, you still modify the original variable.

Pointers are just addresses in memory. Pointers can be used to point to arbitrarily large blocks of data (arrays), or to prevent unnecessary temporaries of an object being created when passing them to functions. They have a range of uses, but it is notoriously easy to mishandle them and corrupt memory, crash your program, leak memory, or unknowingly modify something you didn't mean to.
NextWar: The Quest for Earth available now for Windows Phone 7.
Pointers are difficult to wrap your head around at first. I remember trying to wrap my head around pointers to pointers XD (turns out I was thinking too hard). Pointers are also useful for modifying data through functions.

Quote:Original post by kyron21
Is the only purpose of a pointer to manipulate data without dirrectly touching the variable being pointed to?


Assuming you speak of C++:

Pointers can for instance be used to create array dynamically by assigning a size at run-time, for example:

int size = 0;std::cout << "Enter an array size: ";std::cin >> size;int* a;a = new int[size];// insert code to do with the arraydelete[] a;a = 0;


Another use (albeit can also be done by references) is to pass pointers into a function and "return" into multiple variables, like so:
void function(int* a, int* b){    *a = 1;    *b = 2;}


These are just two examples of the uses of pointers, obviously there are many other uses.
Sc4Freak, I understand all the problems that come along with them. I'm really trying to understand why use them. Do they save memory space (When I say save, I mean not use.....except when calling new). My text book and cplusplus.com tutorials are not helping much with my problem. I'm about to go and pull some old books out from under the bed and go through those as well. I haven't gotten my grade back yet, but I'm betting my last weeks project suffered due to my misunderstanding of pointers. Thanks for your input. It's been helpful already.
ExcessNeo, ah YES. That's what I used them for in my project last week. To setup an array size based on user input. Normally I use vectors and a loop for that....I'm guessing doing it that way saves some effort. I completely understand references. I've used them for quite some time...but pointers are just giving me a hard time. I'm going to experiment more with them and functions I guess. Thank you.
Quote:Original post by kyron21
ExcessNeo, ah YES. That's what I used them for in my project last week. To setup an array size based on user input. Normally I use vectors and a loop for that....I'm guessing doing it that way saves some effort. I completely understand references. I've used them for quite some time...but pointers are just giving me a hard time. I'm going to experiment more with them and functions I guess. Thank you.


If you understand references, then pointers are a kind of mutable reference. Using the pointer's name with no star reassigns what it points to, and using the star accesses the thing it points to (like what a reference does with no star). That means I can change what a pointer points to if I want to. This is useful if you want to do linked lists, for instance, or build a graph or tree.
If you understand references then understanding pointers isnt going to be much of a stretch. Think of a pointer as a reference that can be altered to point to anything.

Referenced are initialised to reference an object and can not be changed to reference something else. Pointers on the other hand can have a null value, can point to an object in memory, and also be altered at run time to point to another object in memory or even some random address (Which of course is not a good idea).
Quote:Original post by furby100
If you understand references, then pointers are a kind of mutable reference. Using the pointer's name with no star reassigns what it points to, and using the star accesses the thing it points to (like what a reference does with no star). That means I can change what a pointer points to if I want to. This is useful if you want to do linked lists, for instance, or build a graph or tree.


What's freaky is I understand what's going on with that link you put in there. I can read the code and follow what pointers are doing I guess...I have a very hard time using pointers myself. But what you said about comparing them to a reference makes sense too. Woohoo. I'm getting closer to understanding pointers, I think. Thanks guys. You've all helped a lot.
TADA! DING!!! I finally got it now.

int* a = &b is not the same as
*a = &b because
the first expressions would be like
a = &b
I believe that was a lot of my confusion as to why I couldn't get them to work properly. 'a' is like a variable. '*a' is what 'a' is pointing to isn't it?
I hope I'm right. It seems right. my codes are working correctly now.

This topic is closed to new replies.

Advertisement