What is the POINT of using pointers?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1240 Views - Last Post: 30 March 2010 - 06:11 AM Rate Topic: -----

#1 killcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 24-March 10

What is the POINT of using pointers?

Posted 28 March 2010 - 10:38 AM

Yeah, What is the POINT of using pointers in C++? like what is the purpose of it? what are they handy for? ect.

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: What is the POINT of using pointers?

#2 nmeans73  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 59
  • Joined: 26-December 09

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 11:08 AM

Pointers are useful for many different things. For one, they are used as a means to access data in some of the STL data structures such as the list. They are also very useful because they allow for the creation of dynamic arrays. Instead of having to a hard cap on the array, you can use a pointer to dynamically allocate a new array that is just the right size for the job that you need.
Was This Post Helpful? 2
  • +
  • -

#3 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 11:23 AM

There's another thread with an excellent post by gregoryH right here that should answer your question.
Was This Post Helpful? 0
  • +
  • -

#4 killcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 24-March 10

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 11:52 AM

Ok thanks man :D
Was This Post Helpful? 0
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 12:02 PM

Another huge benifit of pointers is the abilty to be able to use indirect addressing. For example, lets say you needed to pass a structure to a function and the structure was huge, passing it in as an argument would usually make a copy of the entire structure and is not necessary, so instead you can simply pass a reference (pointer i.e it's address).

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

Was This Post Helpful? 3
  • +
  • -

#6 hlemke  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 18-January 10

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 12:23 PM

It's also helpful when you're working with inheritance and virtual functions. A virtual function is a function that each descendant in an inheritance chain has its own unique copy of. Using pointers, you can create an array of objects of different types that are descended from the same parent class, and then use a pointer to the parent class to call the same function for all of them, and it will actually call each object's unique copy of a function, rather than the parent's function. I guess this probably sounds pretty confusing, but it's extremely helpful if you are, for example, making a video game; in your main game loop, you can have a dynamically sized (another application of pointers!) array of all different sorts of objects, and then just run along the array in a loop calling a virtual function update(), and every game object will happily do whatever it's supposed to do in a given step without having to be given specific instructions, and without you as the developer having to know exactly how many of a given object type will be on the screen at the same time.

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.
Was This Post Helpful? 1
  • +
  • -

#7 homemade-jam  Icon User is offline

  • Gabe's Nemesis
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,300
  • Joined: 17-March 08

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 01:40 PM

Pointers are so powerful it's unreal...although very easy to get wrong and mess up something that is syntactically correct. GDB is your friend.

I'm just going to leave this here Posted Image

This post has been edited by homemade-jam: 28 March 2010 - 01:41 PM

Was This Post Helpful? 2
  • +
  • -

#8 killcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 24-March 10

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 03:20 PM

View Posthlemke, on 28 March 2010 - 11:23 AM, said:

It's also helpful when you're working with inheritance and virtual functions. A virtual function is a function that each descendant in an inheritance chain has its own unique copy of. Using pointers, you can create an array of objects of different types that are descended from the same parent class, and then use a pointer to the parent class to call the same function for all of them, and it will actually call each object's unique copy of a function, rather than the parent's function. I guess this probably sounds pretty confusing, but it's extremely helpful if you are, for example, making a video game; in your main game loop, you can have a dynamically sized (another application of pointers!) array of all different sorts of objects, and then just run along the array in a loop calling a virtual function update(), and every game object will happily do whatever it's supposed to do in a given step without having to be given specific instructions, and without you as the developer having to know exactly how many of a given object type will be on the screen at the same time.

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

Was This Post Helpful? 0
  • +
  • -

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • Joined: 09-June 09

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 03:23 PM

Quote

making unlimited array
i dont know about unlimited, dont wanna corrupt the stack :)
Was This Post Helpful? 1
  • +
  • -

#10 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 04:32 PM

You might just as well ask, "What is the POINT of using variables in C++?" It's basically the same question.

All computer languages use pointers at some level. C++ is just nice (sadistic?) enough to let you seem them.
Was This Post Helpful? 1
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 09:22 PM

The "point of pointers" is to give programmers a way to reference specific addresses in memory. Like the address where I have allocated a block of dynamic memory, or the address of the next element in my linked list, or the address of the next node a tree or a relation between tables... and on and on and on...

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.
Was This Post Helpful? 2
  • +
  • -

#12 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: What is the POINT of using pointers?

Posted 28 March 2010 - 10:01 PM

View PostNickDMax, on 28 March 2010 - 08:22 PM, said:

The "point of pointers" is to give programmers a way to reference specific addresses in memory. Like the address where I have allocated a block of dynamic memory, or the address of the next element in my linked list, or the address of the next node a tree or a relation between tables... and on and on and on...

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
Was This Post Helpful? 0
  • +
  • -

#13 Knabbers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-March 10

Re: What is the POINT of using pointers?

Posted 29 March 2010 - 07:07 PM

This is only one very small advantage of pointers, and I really don't know why I'm even mentioning it.

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!
Was This Post Helpful? 0
  • +
  • -

#14 masoug  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 181
  • Joined: 30-December 09

Re: What is the POINT of using pointers?

Posted 29 March 2010 - 07:40 PM

Pointers are like a URL, they don't have the ACTUAL data (web page), they just tell you WHERE (oh, go to wherever "google.com" is to find their web page) to go find the information. And some of the functionality for using pointers include changing their "URL" (also known as the "address") and changing the data. Plus, it helps with memory allocation.

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
Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • Joined: 09-June 09

Re: What is the POINT of using pointers?

Posted 29 March 2010 - 08:22 PM

This might help get over the hump of grasping what pointers really are..
#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;
}




Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2