Sign in to follow this  
kyron21

Very basic question about pointers

Recommended Posts

Sc4Freak    643
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.

Share this post


Link to post
Share on other sites
_cdcel    114
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.

Share this post


Link to post
Share on other sites
ExcessNeo    619
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 array

delete[] 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.

Share this post


Link to post
Share on other sites
kyron21    122
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.

Share this post


Link to post
Share on other sites
kyron21    122
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.

Share this post


Link to post
Share on other sites
furby100    102
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.

Share this post


Link to post
Share on other sites
instinKt    149
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).

Share this post


Link to post
Share on other sites
kyron21    122
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.

Share this post


Link to post
Share on other sites
kyron21    122
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.

Share this post


Link to post
Share on other sites
Spoonbender    1258
Quote:
Original post by kyron21'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.


Not "like" a variable. 'a' is a variable. It just so happens to contain an address instead of other data. And yeah, you're right.

Share this post


Link to post
Share on other sites
thedustbustr    191
I just realized that I missed the point of his original question, but since I already typed this out, maybe someone will find it interesting. Here's a brief history lesson, hopefully not too simplified, and mostly correct.

Pointers really a concept from the way processors work. Pointers are your way of accessing memory. You have a value stored in memory, you use a pointer to read or write to the value in that slot of memory.

Processors allow for nested calling of functions, each function with its own variables that only it can access. This is implemented in the processor's hardware as a stack which is somewhere in your processes memory. You can access values that are local to your current scope by keeping track of an offset relative to where the stack is in memory. This stuff is obvious in assembly - you access your first 4 byte local variable by doing the equivalent of C code *(EBP - 4), where EBP is a pointer to where your function's part of the stack is in memory.

Languages that give you direct control over memory, like C, automate the stack stuff. But really, all (almost all?) access to memory is done by a pointer, if you were to examine the actual assembly instructions the processor executes.

Higher level languages don't give you such a fine degree of control over memory, and for most applications, that's probably a good thing, because handling memory access ourselves is very prone to nasty bugs.

Here's a fun article if you are interested in such things. Or go read an x86 tutorial. x86 calling conventions

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this