Tornado Rex Posted November 29, 2006 Share Posted November 29, 2006 Ok, so we're working with pointers in class. Now, I get what pointers do, but what I can't seem to figure out is what the practical purpose behind them is. Maybe it's just because we've only been doing stuff where the pointer could be replaced with a normal variable. So anyway, what are some practical uses behind pointers? ~ Proud Supporter of the Child's Play Charity! | GTANET + Child's Play ~ Link to comment Share on other sites More sharing options...
Jack_Knife Posted November 30, 2006 Share Posted November 30, 2006 I don't know sh*t about C++ or pointers, but I know the answer to your question is arrays. "You can play faster than Al Di Meola and do it with only one pinky, but if you're not listening to what is going on around you, you might as well just shut up" isn't your croth suppose to be erecting when you have an orgasm? Link to comment Share on other sites More sharing options...
Myscha Posted December 1, 2006 Share Posted December 1, 2006 Pointers can be used to handle new data on the fly, so you don't have to have a set number of integers, but instead have the program create new ones automatically. So you can have a user enter the number 50 and have the program create fifty new integers automatically. Link to comment Share on other sites More sharing options...
Barguast Posted December 2, 2006 Share Posted December 2, 2006 I had a hard time understanding pointers to begin with too. Basically, pointers are used so you tell your code 'this is where the data is' rather than 'this is the data'. They help avoid duplication and allow you to use arguments in a function without the code having to create a copy (ie. passing by reference vs. passing by value). This is especially important with arrays since pointers allow you to say where the array is, as opposed to giving it a copy. They're also essential when creating objects dynamically since the code will create the object 'somewhere' in memory, and give you a pointer to tell you where it is. Pointers rock my world. Link to comment Share on other sites More sharing options...
vALKYR Posted December 10, 2006 Share Posted December 10, 2006 Yea well pointers can help a lot but are sometimes a pain in the ass when they just refuse to work, especially in granny-like MFC Applications. Like Barg said, variables with no pointers are basically the value you're looking for. But a variable with a pointer is nothing but "adress" of the value, e.g the bucket where the value is going to be in sooner or later. It's kinda hard to understand and even if you understand it, it's hard to get it done but it's possible. If you have a problem, always try to set any variable to =0; or =1; or so, or else the application crashes if it's the default -22,99994654 * 10 ^ 12 or so Link to comment Share on other sites More sharing options...
facugaich Posted December 12, 2006 Share Posted December 12, 2006 Yeah, the difficult thing about pointers is that even if you know what they are you've no idea what they are useful for. As someone said, they are essential when allocating memory dynamically, which you can use to have an array whose length is only known at run-time. Also, they were used in C to pass a variable by reference to a function, meaning that function can change directly the contents of that variable rather than using a copy (pass by value). In C++ you can use the reference ('&') operator. Be careful, although the "adress of" and "reference" operators are represented by the same symbol they are different. C/C++ is a good language to get to know pointers well I think. They are used mostly in more complicated projects and not, say, a simple adder. Link to comment Share on other sites More sharing options...
K^2 Posted December 14, 2006 Share Posted December 14, 2006 The only reason you don't get pointers is because they usually teach C++ backwards. Pointers are everything. Or everything are pointers. Whichever you prefer. Every variable, internally, is treated like a pointer. Every function call is done with pointers. Passing parameters to functions and storing local variables, all of that is done with stack pointers. If you would start learning programming from Assembly, you'd know nothing but pointers by now. But since you did start backwards, here are some important uses. 1) Arrays. Every array is a pointer. Calling x[n] is equivalent to calling *(x+n). Try it. 2) Dynamic memory allocation. You often don't know how much memory you will need when the program begins. You can ask for more using malloc or new. Later, C++ only. But how do you know where the memory is allocated? The function that performs allocation returns a pointer. 3) Lists and trees. Yes, you can usually do these with arrays, but you'd have to keep allocating and deallocating memory, and that takes time. Imagine a structure struct list{int d, list *next}; This structure contains an integer d and a pointer to the next element in the list. You can also have several pointers to following elements, and then you have a tree instead of a list. Great many uses to these in graphics, databases, and AI. 4) Functions. Every function is a pointer to a place in memory where code for the function is stored. Consider the following code: void increment(int &x){x++; }int main(void){int x=5;void (*foo)(int &);foo=increment;foo(x);} As you may imagine, foo(x) does the same as increment(x). That is because both are just pointers to where the function is actually located. 5) Passing variables. You don't really see how it happens, because registers are used for it, but it is done with a stack pointer. If you are interested in this, get some text on disassembly. Prior to filing a bug against any of my code, please consider this response to common concerns. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now