Sign in to follow this  

Pointers

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Could someone give me some examples of how pointers are used, and why? I am a little fuzzy on why you would use a poiter verses regular variables.

Share this post


Link to post
Share on other sites
When you declare a normal variable you use the stack which is limited in memory. When you use pointers and dynamically allocate memory, you are using the heap which has a LOT more memory.

Pointers are VERY important for Data Structures as well (probably one of the bigger part of programming).

Also you can't do something like swap with normal variables in your function arguments.


void Swap(int x, iny y)
{
int temp;
temp = x;
x = y;
y = temp;
}


Will NOT change x to y and y to x. BUT if you did


void Swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}


It would switch x to y and y to x.

Also, passing a pointer as a parameter is faster than passing it by value.
passing by value:


void MakeMonster(CMonster monster)//Will be slower than using a pointer
{}


this will be faster

void MakeMonster(CMonster *monster)//Faster than passing by value
{}

Share this post


Link to post
Share on other sites
When dynamically allocating memory (like was stated above) you have to use a pointer:

Monster* MonsterStructCreatedOnHeap = new Monster;

Pointers are also used for certin data structures like Linked Lists, whos elements in the data structure aren't in sequence in memory (as opposed to an array) so we can store pointers to the next element we want and can traverse the list in sequence.

Pointers are useful but it takes a little searching to find out why. I had this problem too.

Share this post


Link to post
Share on other sites
doing this:

Monster m;
Monster s;
Monster k;
Monster j;
int which=0;
switch(which)
{
case 0:
m.do();
break;

case 1:
s.do();
break;

//....
case 4:
j.do();
break;
}


can be turned into this with pointers:

Monster m;
Monster s;
Monster k;
Monster j;
Monster *which;
if(which==&m)
{
which=&s;
}
else if(which==&s)
{
which=&k;
}
else if(which==&k)
{
which=&j;
}
else if(which==&j)
{
which=&m;
}
which->do();


Which IMO is easier(I know, im crazy)

Share this post


Link to post
Share on other sites
Quote:
Original post by SumDude

void Swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

Little star thingys in function are unnecessary.

You can assign the address of x to temp and then the address of y to x and then the address of x (stored in temp) to y and accompish the exact same thing with less typing :)

Share this post


Link to post
Share on other sites
Quote:
Original post by jperalta
Quote:
Original post by SumDude

void Swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

Little star thingys in function are unnecessary.

You can assign the address of x to temp and then the address of y to x and then the address of x (stored in temp) to y and accompish the exact same thing with less typing :)


Yes, assuming you give temp the correct type for that (int*).

Of course, assuming C++, you could also do it by passing by reference instead of by pointer.

Of course, assuming C++, you could also just invoke std::swap instead. :P

Share this post


Link to post
Share on other sites

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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