18 Replies - 2598 Views - Last Post: 30 March 2010 - 06:11 AM
#1
What is the POINT of using pointers?
Posted 28 March 2010 - 10:38 AM
Thanks
Replies To: What is the POINT of using pointers?
#2
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 11:08 AM
#3
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 11:23 AM
#4
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 11:52 AM

#5
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 12:02 PM
This can be dangerous though, allowing functions to directly mess with memory address that are important is sometimes a bad thing, but C++ accounts for that with the "const" keyword. for example
void ptrFunction(const structure *struct);
This is just one of many examples of why pointers are indespensible.
PS nmeans73 basically summed up everything I just said in one line

This post has been edited by Aphex19: 28 March 2010 - 12:08 PM
#6
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 12:23 PM
Hopefully that makes some sense; I realize it's a pretty dense paragraph.

Pointers also improve the reusability of code. I wrote a Matrix class at the beginning of this semester that I was able to reuse later to solve a totally different problem without having to edit the class at all, simply because it was dynamically rather than statically sized. You'll quickly grow to love dynamic memory allocation.
#7
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 01:40 PM
I'm just going to leave this here

This post has been edited by homemade-jam: 28 March 2010 - 01:41 PM
#8
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 03:20 PM
hlemke, on 28 March 2010 - 11:23 AM, said:
Hopefully that makes some sense; I realize it's a pretty dense paragraph.

Pointers also improve the reusability of code. I wrote a Matrix class at the beginning of this semester that I was able to reuse later to solve a totally different problem without having to edit the class at all, simply because it was dynamically rather than statically sized. You'll quickly grow to love dynamic memory allocation.
Okay, well i already know how to use Dynamic memory allocation for making unlimited array elements(run-time), without having to change the code every time before compiling. Obviously there is many purposes in using dynamic memory of course =). And thanks for the detailed description man anyways

This post has been edited by killcode: 28 March 2010 - 03:21 PM
#9
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 03:23 PM
Quote

#10
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 04:32 PM
All computer languages use pointers at some level. C++ is just nice (sadistic?) enough to let you seem them.
#11
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 09:22 PM
Pointers allow a program to "reference" and "dereference" memory locations.
Binary numbers are just numbers, just ones and zero. Memory is just a massive field of ones and zeros with no rhyme or reason until we point to some location and say, "There those 32 bits there are the number of people in the company", "those 32 bits there, that is the color of the top right pixel of the screen", "oh, and the bits between here and there represent my favorite LOLCAT picture"... A very large part of how binary numbers become "information" is determined by some programmer pointing at them.
#12
Re: What is the POINT of using pointers?
Posted 28 March 2010 - 10:01 PM
NickDMax, on 28 March 2010 - 08:22 PM, said:
Pointers allow a program to "reference" and "dereference" memory locations.
Binary numbers are just numbers, just ones and zero. Memory is just a massive field of ones and zeros with no rhyme or reason until we point to some location and say, "There those 32 bits there are the number of people in the company", "those 32 bits there, that is the color of the top right pixel of the screen", "oh, and the bits between here and there represent my favorite LOLCAT picture"... A very large part of how binary numbers become "information" is determined by some programmer pointing at them.
That's one of the best descriptions I've seen to visualize the information in memory.
Just add something about "oh, those bits there are just junk left over by another program" to cover issues of printing uninitialized variables and it's perfect!
As I get more into pointers I'm finding it's note AS scary and intimidating as it looked when I first heard about them. It seems like a series of lightbulb "Ah, ha!" moments with each step
#13
Re: What is the POINT of using pointers?
Posted 29 March 2010 - 07:07 PM
Pointers (and references) enable your functions to change variables given to them as arguments. You would not be able to do this very easily without them.
e.g. the function AddGravity(int *ySpeed) needs to change the vertical speed of the entity (whether it be the player, or enemies, or random falling trees) given to it:
void AddGravity(float *ySpeed) { *ySpeed += (float)(1/frameRate*9.80665); //yes, this is accurate (on Earth) }
There are many other ways to do this, such as:
float AddGravity() { return (float)(1/frameRate*9.80665); }
But, what about if more than one value needed changing?
Without using pointers, you would need to return a structure of variables, and this gets messy and memory-inefficient.
I hope this strange post wasn't too embarrassing to read, seeing as you've got this far!
#14
Re: What is the POINT of using pointers?
Posted 29 March 2010 - 07:40 PM
But this doesn't necessarily mean "turn ALL your variables into pointers", both pointers and non-pointers have pros and cons, so use them wisely...

-Masoug
#15
Re: What is the POINT of using pointers?
Posted 29 March 2010 - 08:22 PM
#include <iostream> using namespace std; int main () { int *a = NULL; int b = 10; a = &b; cout<<"A POINTER ADDRESS : "<<a<<endl //address of pointer A equals address of variable B <<"VALUE AT THIS ADDRESS : "<<*a<<endl <<"B POINTER ADDRESS : "<<&b<<endl <<"VAlUE AT THIS ADDRESS : "<<b<<endl; cin.ignore(); cin.get(); return 0; }